﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using WP.Modules;
using WP.DB;
using WP.Controls;
using WP.Commons;
using WP.Controls.GridColumn;
using System.Collections;

namespace WP.IM.Form
{
    public partial class frmImCategroyWKWorkCost : BaseQryRpt 
    {
        public frmImCategroyWKWorkCost()
        {
            InitializeComponent();
            LoadAllControl(new ArrayList(this.Controls));
            this.MinimumSize = this.Size;
            InitGridDtl();
            InitSubGridDtl();
        }
        private bool mPressExit = false;
        private bool mblnSettingStatus = false;
        private bool[] mDtlColumnReadOnlyArray;
        private bool m_bCanTrigerCellValueChangedEven = false;
        private bool m_bSettingStatus = false;
        private bool m_bDtlChanged = false;
        private System.Windows.Forms.DataGridView.HitTestInfo mhtiDtl = null;
        private DBAccess mdbcon = new DBAccess(false, false);
        private string mstrTableName = "imCategoryWKWorkCost";
        private string mstrTableName_Sub = "imCategoryWKWorkCostPosition";
        private bool mblnInDetailMode = false;
        private string mstrCategoryID = "";
        private string mstrWKProductID = "";        
        
        private enum DtlCol
        {
            RowSeqNo=0,
            CategoryID,
            CategoryName,

            /// <summary>
            /// 工序编码
            /// </summary>
            WKProductID,

            /// <summary>
            /// 工序名称
            /// </summary>
            WKName,  

            /// <summary>
            /// 工价类别
            /// </summary>
            WKPriceType,         //工价类别

            /// <summary>
            /// 岗位
            /// </summary>
            WorkPositionSeqNo,

            /// <summary>
            /// 岗位名称
            /// </summary>
            WKPostiton,

            /// <summary>
            /// 额定产能
            /// </summary>
            WKSTDQty,            //额定产能

            /// <summary>
            /// 额定产能单位
            /// </summary>
            STDQtyUnit,          //额定产能单位

            /// <summary>
            /// 固定工资
            /// </summary>
            WKSalary,            //固定工资


            /// <summary>
            /// 标准工资
            /// </summary>
            WKSTDSalary,         //标准工资


            /// <summary>
            /// 计件工价
            /// </summary>
            WKUnitCost,          //计件工价
            /// <summary>
            /// 标准工资是26天的,日工资=标准工资/26天,总工价=日工资/额定产能
            /// </summary>
            WKTotUnitCost,       //总工价   
            WKRemark,       
            WorkCost,
            /// <summary>
            /// 此岗位工人数量
            /// </summary>
            WorkersQty ,   
            RecID,
            ValueChanged,
            DelButton,
            HdrRecID
        }

        protected override bool ProcessCmdKey(ref System.Windows.Forms.Message msg, System.Windows.Forms.Keys keyData) //激活回车键
        {
            int WM_KEYDOWN = 256;
            int WM_SYSKEYDOWN = 260;
            if (!(msg.Msg == WM_KEYDOWN | msg.Msg == WM_SYSKEYDOWN))
            {
                return false;
            }

            bool blnRet = false;
            if (BaseFunctions.TrigerKeyEventByControl(this.ActiveControl, this.dgvDetail.Name))
            {
                blnRet = dgvDetail_KeyDownEvent(keyData);
            }
            if (BaseFunctions.TrigerKeyEventByControl(this.ActiveControl, this.dgvSubDetail.Name))
            {
                blnRet = dgvSubDetail_KeyDownEvent(keyData);
            }         

            if (!blnRet)
            {
                return base.ProcessCmdKey(ref msg, keyData);
            }
            return blnRet;
        }

        #region Detail GRID

        private void InitGridDtl()
        {

            this.dgvDetail.Columns.Clear();
            this.dgvDetail.Rows.Clear();

            DataGridViewTextBoxColumn colRowSeqNo = new DataGridViewTextBoxColumn();
            colRowSeqNo.HeaderText = "序号";
            colRowSeqNo.Visible = true;
            colRowSeqNo.Name = "RowSeqNo";
            colRowSeqNo.Width = 50;
            colRowSeqNo.ReadOnly = true;
            colRowSeqNo.SortMode = DataGridViewColumnSortMode.NotSortable;
            
            TextLookUpColumn colCategoryID = new TextLookUpColumn();
            colCategoryID.HeaderText = "类别编码";
            colCategoryID.Width = 70;
            colCategoryID.Name = "CategoryID";
            colCategoryID.ReadOnly = false;
            colCategoryID.Visible = true;

            TextLookUpColumn colCategoryName = new TextLookUpColumn();
            colCategoryName.HeaderText = "类别名称";
            colCategoryName.Width = 100;
            colCategoryName.Name = "CategoryName";
            colCategoryName.ReadOnly = true   ;
            colCategoryName.Visible = true ;

            TextLookUpColumn colWKID = new TextLookUpColumn();
            colWKID.HeaderText = "工序编码";
            colWKID.Width = 70;
            colWKID.Name = "WKProductID";            
            colWKID.ReadOnly = false;
            colWKID.SortMode = DataGridViewColumnSortMode.Automatic;

            TextLookUpColumn colWKName = new TextLookUpColumn();
            colWKName.HeaderText = "工序名称";
            colWKName.Width = 100;
            colWKName.Name = "WKName";
            colWKName.ReadOnly = false;
            colWKName.SortMode = DataGridViewColumnSortMode.Automatic;


            //WKPrictType,         //工价类别
            DataGridViewTextBoxColumn colWKPrictType = new DataGridViewTextBoxColumn();
            colWKPrictType.HeaderText = "工价类别";
            colWKPrictType.Width = 100;
            colWKPrictType.Name = "WKPriceType";
            colWKPrictType.ReadOnly =false  ;

            //WKPosition,         //岗位SeqNo
            DataGridViewTextBoxColumn colWorkPositionSeqNo = new DataGridViewTextBoxColumn();
            colWorkPositionSeqNo.HeaderText = "岗位";
            colWorkPositionSeqNo.Width = 100;
            colWorkPositionSeqNo.Name = "WorkPositionSeqNo";
            colWorkPositionSeqNo.ReadOnly = false;
            colWorkPositionSeqNo.Visible = false;

            //WKPosition,         //岗位名称
            DataGridViewTextBoxColumn colWKPosition = new DataGridViewTextBoxColumn();
            colWKPosition.HeaderText = "岗位";
            colWKPosition.Width = 100;
            colWKPosition.Name = "WKPosition";
            colWKPosition.ReadOnly = false;
            colWKPosition.Visible = false;
            
            //WKSTDQty,            //额定产能
            DataGridViewTextBoxColumn colWKSTDQty = new DataGridViewTextBoxColumn();
            colWKSTDQty.HeaderText = "额定产能";
            colWKSTDQty.Width = 100;
            colWKSTDQty.Name = "WKSTDQty";
            colWKSTDQty.ReadOnly = false;
            colWKSTDQty.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight;
            colWKSTDQty.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;

            //STDQtyUnit
            DataGridViewTextBoxColumn colSTDQtyUnit = new DataGridViewTextBoxColumn();
            colSTDQtyUnit.HeaderText = "单位";
            colSTDQtyUnit.Width = 50;
            colSTDQtyUnit.Name = "STDQtyUnit";
            colSTDQtyUnit.ReadOnly = false;
            colSTDQtyUnit.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight;
            colSTDQtyUnit.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;

            //WKSalary,            //固定工资
            DataGridViewTextBoxColumn colWKSalary = new DataGridViewTextBoxColumn();
            colWKSalary.HeaderText = "固定工资";
            colWKSalary.Width = 100;
            colWKSalary.Name = "WKSalary";
            colWKSalary.ReadOnly = false;
            colWKSalary.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight;
            colWKSalary.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;

            //WKSTDSalary,         //标准工资
            DataGridViewTextBoxColumn colWKSTDSalary = new DataGridViewTextBoxColumn();
            colWKSTDSalary.HeaderText = "标准工资";
            colWKSTDSalary.Width = 100;
            colWKSTDSalary.Name = "WKSTDSalary";
            colWKSTDSalary.ReadOnly = false;
            colWKSTDSalary.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight;
            colWKSTDSalary.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;

            //WKUnitCost     //计件工价
            DataGridViewTextBoxColumn colWKUnitCost = new DataGridViewTextBoxColumn();
            colWKUnitCost.HeaderText = "计件工价";
            colWKUnitCost.Width = 100;
            colWKUnitCost.Name = "WKUnitCost";
            colWKUnitCost.ReadOnly = false;
            colWKUnitCost.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight;
            colWKUnitCost.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;

            //WKTotUnitCost     //总工价
            DataGridViewTextBoxColumn colWKTotUnitCost = new DataGridViewTextBoxColumn();
            colWKTotUnitCost.HeaderText = "总工价";
            colWKTotUnitCost.Width = 100;
            colWKTotUnitCost.Name = "WKTotUnitCost";
            colWKTotUnitCost.ReadOnly = false;
            colWKTotUnitCost.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight;
            colWKTotUnitCost.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;


            DataGridViewTextBoxColumn colWKRemark = new DataGridViewTextBoxColumn();
            colWKRemark.HeaderText = "工序描述";
            colWKRemark.Width = 100;
            colWKRemark.Name = "WKRemark";
            colWKRemark.ReadOnly = true;

            DataGridViewTextBoxColumn colWorkCost = new DataGridViewTextBoxColumn();
            colWorkCost.HeaderText = "工时费用标准";
            colWorkCost.Width = 80;
            colWorkCost.Name = "WorkCostRate";
            colWorkCost.ReadOnly = false ;
            colWorkCost.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight ;
            colWorkCost.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;

            DataGridViewTextBoxColumn colWorkersQty = new DataGridViewTextBoxColumn();
            colWorkersQty.HeaderText = "定编人数";
            colWorkersQty.Width = 80;
            colWorkersQty.Name = "WorkersQty";
            colWorkersQty.ReadOnly = false;
            colWorkersQty.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight;
            colWorkersQty.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
            colWorkersQty.Visible = false; 

            DataGridViewTextBoxColumn colRecID = new DataGridViewTextBoxColumn();
            colRecID.HeaderText = "RecID";
            colRecID.Visible = false;
            colRecID.Name = "RecID";

            DataGridViewTextBoxColumn colValueChanged = new DataGridViewTextBoxColumn();
            colValueChanged.HeaderText = "ValueChanged";
            colValueChanged.Visible = false;
            colValueChanged.Name = "ValueChanged";

            DataGridViewImageColumn colDel = new DataGridViewImageColumn();
            Bitmap delbitmap = new Bitmap(this.iglSource.Images["del.ico"]);
            colDel.Width = 20;
            colDel.Name = "DelLine";
            colDel.HeaderText = "";
            colDel.Resizable = DataGridViewTriState.False;

            this.dgvDetail.Columns.AddRange(new DataGridViewColumn[] { colRowSeqNo, colCategoryID, colCategoryName, colWKID, colWKName, colWKPrictType, 
                                                                       colWorkPositionSeqNo,colWKPosition, colWKSTDQty, colSTDQtyUnit, colWKSalary, colWKSTDSalary, colWKUnitCost, 
                                                                       colWKTotUnitCost, colWKRemark, colWorkCost,colWorkersQty,colRecID, colValueChanged, colDel });

            mDtlColumnReadOnlyArray = new bool[dgvDetail.Columns.Count];
            string[] ColumnWidthArray = DataFunctions.GetGridColumnWidthString(this.Name, this.CurrentMenuInfo.iParameter, SysVar.UserName, this.Name + "." + this.dgvDetail.Name).Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
            for (int i = 0; i < dgvDetail.Columns.Count; i++)
            {
                mDtlColumnReadOnlyArray[i] = dgvDetail.Columns[i].ReadOnly;
                if (ColumnWidthArray.Length == dgvDetail.Columns.Count) dgvDetail.Columns[i].Width = CvtFunc.ToInt(ColumnWidthArray[i]);
            }

            this.dgvDetail.EditMode = DataGridViewEditMode.EditOnEnter;
            this.dgvDetail.RowTemplate.Height = 20;
            this.dgvDetail.RowHeadersWidth = 23;
            this.dgvDetail.RowHeadersBorderStyle = DataGridViewHeaderBorderStyle.None;
            this.dgvDetail.RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.DisableResizing;
            this.dgvDetail.ColumnHeadersHeight = 30;
            this.dgvDetail.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing;
            this.dgvDetail.SelectionMode = DataGridViewSelectionMode.CellSelect;
            this.dgvDetail.AllowUserToResizeRows = false;

        }
        private void FillDtl(string strCategoryName)
        {
            string strSql = "";
            string strUserName = "";
            string strRecid = "";
            bool bAccess = false;
            m_bCanTrigerCellValueChangedEven = false;

            this.dgvDetail.Rows.Clear();
            DBAccess dbCon = new DBAccess(false, false);

            if (strCategoryName == "ALL")
            {
                strSql = "select A.*,B.WorkingProcedureName,b.Remark from " + mstrTableName + " A inner join rmWorkingProcedure B on A.WKProductID=B.WorkingProcedureID order by A.CategoryID, A.WKProductID";
            }
            else
            {
                strSql = " select A.*,B.WorkingProcedureName,b.Remark from " + mstrTableName + " A inner join rmWorkingProcedure B on A.WKProductID=B.WorkingProcedureID " +
                    " where CategoryID='" + mstrCategoryID + "' and " + (mstrWKProductID == "" ? "1=1" : "WKProductID='" + mstrWKProductID + "'") + " order by A.CategoryID, A.WKProductID";
            }
            DataTable mUserAlertTable = dbCon.GetDataSet(strSql).Tables[0];

            for (int i = 0; i < mUserAlertTable.Rows.Count; i++)
            {
                DataRow dr = mUserAlertTable.Rows[i];
                if (this.dgvDetail.Rows.Count <= i) this.dgvDetail.Rows.Add();
                dgvDetail.Rows[dgvDetail.Rows.Count - 1].Cells[(int)DtlCol.RowSeqNo].Value = dgvDetail.Rows.Count.ToString();
                dgvDetail.Rows[dgvDetail.Rows.Count - 1].Cells[(int)DtlCol.WKProductID].Value = dr["WKProductID"].ToString().TrimEnd();
                dgvDetail.Rows[dgvDetail.Rows.Count - 1].Cells[(int)DtlCol.WKName].Value = dr["WorkingProcedureName"].ToString().TrimEnd();
                dgvDetail.Rows[dgvDetail.Rows.Count - 1].Cells[(int)DtlCol.WKRemark].Value = dr["Remark"].ToString().TrimEnd();
                dgvDetail.Rows[dgvDetail.Rows.Count - 1].Cells[(int)DtlCol.CategoryID].Value = dr["CategoryID"].ToString().TrimEnd();                
                dgvDetail.Rows[dgvDetail.Rows.Count - 1].Cells[(int)DtlCol.CategoryName].Value = dr["CategoryName"].ToString().TrimEnd();

                dgvDetail.Rows[dgvDetail.Rows.Count - 1].Cells[(int)DtlCol.WKPriceType].Value = CvtFunc.ToString (dr["WKPriceType"].ToString());
                dgvDetail.Rows[dgvDetail.Rows.Count - 1].Cells[(int)DtlCol.WKSTDQty].Value = BaseFunctions.FormatNumeric(dr["WKSTDQty"].ToString(), "Ns.2");
                dgvDetail.Rows[dgvDetail.Rows.Count - 1].Cells[(int)DtlCol.STDQtyUnit].Value = CvtFunc.ToString (dr["STDQtyUnit"].ToString());

                
                dgvDetail.Rows[dgvDetail.Rows.Count - 1].Cells[(int)DtlCol.WKSalary].Value = BaseFunctions.FormatNumeric(dr["WKSalary"].ToString(), "Ns.2");
                dgvDetail.Rows[dgvDetail.Rows.Count - 1].Cells[(int)DtlCol.WKSTDSalary].Value = BaseFunctions.FormatNumeric(dr["WKSTDSalary"].ToString(), "Ns.2");
                dgvDetail.Rows[dgvDetail.Rows.Count - 1].Cells[(int)DtlCol.WKUnitCost].Value = BaseFunctions.FormatNumeric(dr["WKUnitCost"].ToString(), "Ns.6");
                dgvDetail.Rows[dgvDetail.Rows.Count - 1].Cells[(int)DtlCol.WKTotUnitCost].Value = BaseFunctions.FormatNumeric(dr["WKTotUnitCost"].ToString(), "Ns.6");
                
                dgvDetail.Rows[dgvDetail.Rows.Count - 1].Cells[(int)DtlCol.WorkCost].Value = BaseFunctions.FormatNumeric(dr["WorkCostRate"], "ns.2"); 
                dgvDetail.Rows[dgvDetail.Rows.Count - 1].Cells[(int)DtlCol.RecID].Value = CvtFunc.ToInt(dr["RecID"]);
                dgvDetail.Rows[dgvDetail.Rows.Count - 1].Cells[(int)DtlCol.ValueChanged].Value = "0";
            }//
            if (dgvDetail.Rows.Count == 0)
            {
                //AddDtlRow(this.dgvDetail.Name);
            }
            dbCon.CloseCon();
            FillSubDtl(0); 
            base.SetBlankImage(dgvDetail);
            m_bCanTrigerCellValueChangedEven = true;
        }
        private void dgvDetail_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

            if (dgvDetail.Columns[e.ColumnIndex] is TextLookUpColumn)
            {
                if ((dgvDetail.Columns[e.ColumnIndex] as TextLookUpColumn).CanLookUp())
                {
                    Dtl_LookUp(e.ColumnIndex);
                }
            }
            else
            {
                if (this.dgvDetail.CurrentCell.ColumnIndex == (int)DtlCol.DelButton && base.HasDelImage(this.dgvDetail, e))
                {
                    this.dgvDetail.CurrentCell.Selected = true;
                    this.dgvDetail.CurrentCell.Selected = false;
                    if (!this.Delete_Detail(this.dgvDetail.Rows[this.dgvDetail.CurrentCell.RowIndex]))
                    {
                        return;
                    }
                    this.dgvDetail.Rows.Remove(this.dgvDetail.Rows[this.dgvDetail.CurrentCell.RowIndex]);
                    if (this.dgvDetail.Rows.Count == 0)
                    {
                        this.AddDtlRow(this.dgvDetail.Name);
                    }
                }
            }
        }
        private void AddDtlRow(string strGridName)
        {
            AddDtlRow(strGridName, -1);
        }

        private void AddDtlRow(string strGridName, double iRow)
        {
            if (strGridName == dgvDetail.Name)
            {
                int intRow = CvtFunc.ToInt(iRow);
                m_bCanTrigerCellValueChangedEven = false;
                if (iRow >= 0)
                {
                    intRow++;
                    this.dgvDetail.Rows.Insert(intRow, 1);
                }
                else
                {
                    this.dgvDetail.Rows.Add();
                    intRow = this.dgvDetail.Rows.Count - 1;
                }
                dgvDetail.Rows[intRow].Cells[(int)DtlCol.RowSeqNo].Value = dgvDetail.Rows.Count.ToString();
                dgvDetail.Rows[intRow].Cells[(int)DtlCol.CategoryID].Value = mstrCategoryID;
                dgvDetail.Rows[intRow].Cells[(int)DtlCol.CategoryName].Value = DataFunctions.GetFieldValue("rmCategory", "CategoryName", "CategoryID='" + mstrCategoryID + "'") ;
                dgvDetail.Rows[intRow].Cells[(int)DtlCol.WorkCost].Value = "0.00";
                dgvDetail.Rows[intRow].Cells[(int)DtlCol.WKRemark].Value = "";
                dgvDetail.Rows[intRow].Cells[(int)DtlCol.WKProductID].Value = mstrWKProductID ;
                dgvDetail.Rows[intRow].Cells[(int)DtlCol.WKName].Value = DataFunctions.GetFieldValue("rmWorkingProcedure", "WorkingProcedureName", "WorkingProcedureID='" + mstrWKProductID + "'");

                dgvDetail.Rows[intRow].Cells[(int)DtlCol.WKPriceType].Value = "";
                dgvDetail.Rows[intRow].Cells[(int)DtlCol.WKSalary].Value = "0";
                dgvDetail.Rows[intRow].Cells[(int)DtlCol.WKSTDQty].Value = "0";
                dgvDetail.Rows[intRow].Cells[(int)DtlCol.STDQtyUnit].Value = "";
                dgvDetail.Rows[intRow].Cells[(int)DtlCol.WKSTDSalary].Value = "0";
                dgvDetail.Rows[intRow].Cells[(int)DtlCol.WKUnitCost].Value = "0.000000";
                dgvDetail.Rows[intRow].Cells[(int)DtlCol.WKTotUnitCost].Value = "0.000000";
                dgvDetail.Rows[intRow].Cells[(int)DtlCol.RecID].Value = -1;
                dgvDetail.Rows[intRow].Cells[(int)DtlCol.ValueChanged].Value = "0";
                dgvDetail.CurrentCell = dgvDetail.Rows[intRow].Cells[(int)DtlCol.CategoryID];
                m_bCanTrigerCellValueChangedEven = true;
            }
        }
        private void Dtl_LookUp(int iColumnIndex)
        {
            string strSQL = "", strCaption = "", strFindValue = "", strFindField = "";
            switch ((DtlCol)iColumnIndex)
            {
                case DtlCol.WKProductID :
                    strSQL = WP.Commons.BaseFunctions.GetLookUpSQL("rmWorkingProcedure");
                    strCaption = "工序选择";
                    if (this.dgvDetail.CurrentRow.Cells[(int)DtlCol.WKName].Value != null) strFindValue = this.dgvDetail.CurrentRow.Cells[(int)DtlCol.WKName].Value.ToString();
                    strFindField = "WorkingProcedureID";
                    break;
                case DtlCol.CategoryID :
                    strSQL = WP.Commons.BaseFunctions.GetLookUpSQL("rmCategory");
                    strCaption = "类别选择";
                    if (this.dgvDetail.CurrentRow.Cells[(int)DtlCol.CategoryID].Value != null) strFindValue = this.dgvDetail.CurrentRow.Cells[(int)DtlCol.CategoryID].Value.ToString();
                    strFindField = "CategoryID";
                    break;

            }

            if (strSQL == "") return;
            if (mblnLookUpSortByDescription)
            {
                strSQL = DataFunctions.GetSqlByDescriptionSort(strSQL);
                mblnLookUpSortByDescription = false;
            }

            frmLookUp frmlookup = new frmLookUp(strSQL, strCaption, strFindValue, strFindField);
            frmlookup.ShowDialog();

            if (frmlookup.HaveReturnValue)
            {
                switch ((DtlCol)iColumnIndex)
                {
                    case DtlCol.WKProductID :
                        this.dgvDetail.CurrentRow.Cells[(int)DtlCol.WKProductID].Value = frmlookup.GetReturnValue("WorkingProcedureID");
                        this.dgvDetail.CurrentRow.Cells[(int)DtlCol.WKName].Value = frmlookup.GetReturnValue("WorkingProcedureName");
                        this.dgvDetail.CurrentRow.Cells[(int)DtlCol.WKRemark].Value = frmlookup.GetReturnValue("Remark");
                        break;
                    case DtlCol.CategoryID :
                        this.dgvDetail.CurrentRow.Cells[(int)DtlCol.CategoryID].Value = frmlookup.GetReturnValue("CategoryID");   
                        this.dgvDetail.CurrentRow.Cells[(int)DtlCol.CategoryName].Value = frmlookup.GetReturnValue("CategoryName");                        
                        break;
                }
            }
            frmlookup.Close();
        }
        private bool CheckDtlRow(ref string strErrMsg)
        {
            bool bCheckResult = true;
            for (int i = 0; i < dgvDetail.ColumnCount - 1; i++)
            {

                bCheckResult = CheckDtlCell(i, ref strErrMsg);
                if (!bCheckResult) break;
            }
            string strRecID= CvtFunc.ToString ( this.dgvDetail.CurrentRow.Cells [(int)DtlCol.RecID ].Value) ;
            string strCategoryName = CvtFunc.ToString(this.dgvDetail.CurrentRow.Cells[(int)DtlCol.CategoryID].Value);
            string strWKName = CvtFunc.ToString(this.dgvDetail.CurrentRow.Cells[(int)DtlCol.WKProductID].Value);
            string strWKPriceType = CvtFunc.ToString(this.dgvDetail.CurrentRow.Cells[(int)DtlCol.WKPriceType].Value);
            string strSQL = "select * from " + mstrTableName + " where RecID!='" + strRecID + "' and CategoryID='" + strCategoryName + "' and WKProductID='" + strWKName + "' and WKPriceType='"+ strWKPriceType +"'";
            DataRow drCheck = mdbcon.GetDataFirstRow(strSQL);
            if (drCheck != null)
            {
                strErrMsg = "此设置已经存在，不能重复添加。";
                return false;
            }
            return bCheckResult;
        }
        private bool CheckDtlCell(int iColumn, ref string strErrMsg)
        {
            bool bReturnValue = true;
            strErrMsg = "";
            string strValue = "";
            if (this.dgvDetail.CurrentRow != null && this.dgvDetail.CurrentRow.Cells[iColumn].Value != null)
            {
                strValue = this.dgvDetail.CurrentRow.Cells[iColumn].Value.ToString().TrimEnd();
            }
            switch ((DtlCol)iColumn)
            {
                case DtlCol.CategoryName:
                    if (strValue.Trim() == "")
                    {
                        strErrMsg = "类别名称不能空.";
                        return false;
                    }
                    if (!DataFunctions.CheckRecordExist("rmCategory", strValue, "CategoryName"))
                    {
                        strErrMsg = "不存在的类别名称";
                        return false;
                    }
                    return true;
                case DtlCol.WKProductID:
                    if (strValue.Trim() == "")
                    {
                        strErrMsg = "工序不能空.";
                        return false;
                    }
                    if (!DataFunctions.CheckRecordExist("rmWorkingProcedure", strValue, "WorkingProcedureID"))
                    {
                        strErrMsg = "不存在的工序";
                        return false;
                    }
                    return true;
                case DtlCol.WKName:
                    if (strValue.Trim() == "")
                    {
                        strErrMsg = "工序名称不能空.";
                        return false;
                    }
                    if (!DataFunctions.CheckRecordExist("rmWorkingProcedure", strValue, "WorkingProcedureName"))
                    {
                        strErrMsg = "不存在的工序名称";
                        return false;
                    }
                    return true;
            }
            return bReturnValue;
        }
        private void dgvDetail_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
        {
            if (dgvDetail.CurrentRow == null)
            {
                e.Cancel = false;
                return;
            }
            if (CvtFunc.ToInt(dgvDetail.CurrentRow.Cells[(int)DtlCol.RecID].Value) > 0 && CvtFunc.ToInt(dgvDetail.CurrentRow.Cells[(int)DtlCol.ValueChanged].Value) == 0)
            {
                e.Cancel = false;
                return;

            }

            if (mhtiDtl != null && mhtiDtl.ColumnIndex == (int)DtlCol.DelButton)
            {
                mhtiDtl = null;
                return;
            }


            if (this.dgvDetail.CurrentCell.ColumnIndex == (int)DtlCol.DelButton)
            {
                e.Cancel = false;
                return;

            }
            string strErrMsg = "";
            if (!BaseFunctions.NeedValidGridCell(this.ActiveControl, "dgvDetail") || m_bSettingStatus || !m_bCanTrigerCellValueChangedEven)
            {
                e.Cancel = false;
                return;
            }

            e.Cancel = !CheckDtlCell(e.ColumnIndex, ref strErrMsg);
            if (mPressExit) { e.Cancel = false ; return; };
            if (strErrMsg != "")
            {
                MessageBox.Show(strErrMsg, SysVar.WarnTitle, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }
        private void dgvDetail_RowValidating(object sender, DataGridViewCellCancelEventArgs e)
        {
            string strErrMsg = "";
            if (dgvDetail.CurrentRow != null && CvtFunc.ToInt(dgvDetail.CurrentRow.Cells[(int)DtlCol.RecID].Value) > 0 && CvtFunc.ToInt(dgvDetail.CurrentRow.Cells[(int)DtlCol.ValueChanged].Value) == 0)
            {
                e.Cancel = false;                
                return;
            }

           
            if (!BaseFunctions.NeedValidGridCell(this.ActiveControl, "dgvDetail") || m_bSettingStatus || !m_bCanTrigerCellValueChangedEven)
            {
                e.Cancel = false;
                return;
            }
            e.Cancel = !CheckDtlRow(ref strErrMsg);
            if (mPressExit) { e.Cancel = false ; return; };
            if (this.dgvSubDetail.Rows.Count > 0)
            {
                if (!CheckSubDtlRow(ref strErrMsg))
                {
                    if (strErrMsg != "")
                    {
                        MessageBox.Show(strErrMsg, SysVar.WarnTitle, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        e.Cancel = true;
                    }
                }
            }
            if (strErrMsg != "")
            {
                MessageBox.Show(strErrMsg, SysVar.WarnTitle, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            
        }
        private void dgvDetail_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            if (!m_bCanTrigerCellValueChangedEven || e.ColumnIndex == (int)DtlCol.ValueChanged || e.ColumnIndex == (int)DtlCol.DelButton) return;
            dgvDetail.Rows[e.RowIndex].Cells[(int)DtlCol.ValueChanged].Value = "1";
            m_bDtlChanged = true;
            dgvDetail.EndEdit();
            if (e.ColumnIndex == (int)DtlCol.CategoryID)
            {
                string strCategoryID = CvtFunc.ToString(this.dgvDetail.CurrentRow.Cells[(int)DtlCol.CategoryID].Value);
                this.dgvDetail.CurrentRow.Cells[(int)DtlCol.CategoryName].Value = DataFunctions.GetFieldValue("rmCategory", "CategoryName", "CategoryID='" + strCategoryID + "'");
            }
            if (e.ColumnIndex == (int)DtlCol.WKProductID)
            {
                string strWKName = CvtFunc.ToString(this.dgvDetail.CurrentRow.Cells[(int)DtlCol.WKProductID].Value);
                this.dgvDetail.CurrentRow.Cells[(int)DtlCol.WKName].Value = DataFunctions.GetFieldValue("rmWorkingProcedure", "WorkingProcedureName", "WorkingProcedureID='" + strWKName + "'");
                this.dgvDetail.CurrentRow.Cells[(int)DtlCol.WKRemark].Value = DataFunctions.GetFieldValue("rmWorkingProcedure", "Remark", "WorkingProcedureID='" + strWKName + "'");
            }
            if (e.ColumnIndex == (int)DtlCol.WKSalary || e.ColumnIndex == (int)DtlCol.WKSTDQty || e.ColumnIndex == (int)DtlCol.WKSTDSalary)
            {
                //计件工资：
                //(标准工资-固定工资/22*26）/额定产量/26
                //(dblStandardWage26 - dblStandardWage / 22d * 26) / dblStandardQty / 26d;

                //总工价：
                //标准工资是26天的
                //日工资=标准工资/26天
                //总工价=日工资/额定产能
                
                double dblStandardWage26 =CvtFunc.ToDouble(this.dgvDetail.CurrentRow.Cells[(int)DtlCol.WKSTDSalary].Value);
                double dblStandardWage =CvtFunc.ToDouble(this.dgvDetail.CurrentRow.Cells[(int)DtlCol.WKSalary].Value );
                double dblStandardQty = CvtFunc.ToDouble(this.dgvDetail.CurrentRow.Cells[(int)DtlCol.WKSTDQty].Value);
                if (dblStandardQty == 0)
                {
                    this.dgvDetail.CurrentRow.Cells[(int)DtlCol.WKUnitCost].Value = "0";
                    this.dgvDetail.CurrentRow.Cells[(int)DtlCol.WKTotUnitCost].Value = "0";  
                }
                else
                {
                    this.dgvDetail.CurrentRow.Cells[(int)DtlCol.WKUnitCost].Value =BaseFunctions.FormatNumeric ( (dblStandardWage26 - dblStandardWage / 22d * 26) / dblStandardQty / 26d,"ns.6");
                    this.dgvDetail.CurrentRow.Cells[(int)DtlCol.WKTotUnitCost].Value = BaseFunctions.FormatNumeric(dblStandardWage26/26d/dblStandardQty, "ns.6");
                }
            }
            dgvDetail.EndEdit();
        }
        private void dgvDetail_CellEnter(object sender, DataGridViewCellEventArgs e)
        {
            if (this.dgvDetail.CurrentCell.ColumnIndex == (int)DtlCol.DelButton)
            {
                this.dgvDetail.CurrentCell.Selected = false;
            }
            if ( CvtFunc.ToDouble (this.dgvDetail.Rows[this.dgvDetail.CurrentCell.RowIndex].Cells[(int)DtlCol.RecID].Value) > 0)
            {
                this.dgvDetail.Rows[this.dgvDetail.CurrentCell.RowIndex].Cells[(int)DtlCol.CategoryName].ReadOnly = true;
            }
        }
        private void dgvDetail_CellContentDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (this.dgvDetail.CurrentCell.ColumnIndex == (int)DtlCol.DelButton)
            {
                this.dgvDetail.CurrentCell.Selected = false;
            }
        }
        private void dgvDetail_DataError(object sender, DataGridViewDataErrorEventArgs e)
        {
            return;
        }
        private void dgvDetail_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            base.SetGridViewDelImage(this.dgvDetail, e, (int)DtlCol.DelButton);
            string strErrMsg = "";
            if (dgvDetail.CurrentRow != null && CvtFunc.ToInt(dgvDetail.CurrentRow.Cells[(int)DtlCol.ValueChanged].Value) > 0)
            {
                if (CheckDtlRow(ref strErrMsg))
                {
                    if (!Update_Detail(this.dgvDetail.Rows[this.dgvDetail.CurrentCell.RowIndex]))
                    {
                        return;
                    }
                  
                     //AddDtlRow(this.dgvDetail.Name);
                }
                else
                {
                    return;
                }
            }
            //if (this.dgvSubDetail.Rows.Count > 0)
            //{               
            //    if (!CheckSubDtlRow(ref strErrMsg))
            //    {
            //        MsgBoxEx.Show(strErrMsg);
            //        //this.dgvDetail.CurrentCell = this.dgvDetail.Rows[e.RowIndex].Cells[e.ColumnIndex];
            //        return;
            //    }
            //}
            if (this.dgvDetail.Enabled)
            {
                FillSubDtl(e.RowIndex);
            }
        }
        private void dgvDetail_MouseDown(object sender, MouseEventArgs e)
        {

            mhtiDtl = this.dgvDetail.HitTest(e.X, e.Y);

            //System.Windows.Forms.DataGridView.HitTestInfo hti;
            //hti = this.dgvDetail.HitTest(e.X, e.Y);
            //switch (hti.Type)
            //{
            //    case System.Windows.Forms.DataGridViewHitTestType.Cell:
            //        break;
            //}
        }
        private bool dgvDetail_KeyDownEvent(System.Windows.Forms.Keys keyData)
        {
            string strErrMsg = "";
            int j;
            switch (keyData)
            {
                case Keys.Down:
                    #region Key Down

                    this.dgvDetail.EndEdit();
                    if (CvtFunc.ToInt(dgvDetail.CurrentRow.Cells[(int)DtlCol.RecID].Value) > 0)
                    {
                        if (CvtFunc.ToInt(dgvDetail.CurrentRow.Cells[(int)DtlCol.ValueChanged].Value) > 0)
                        {

                            if (CheckDtlRow(ref strErrMsg))
                            {

                                if (!Update_Detail(this.dgvDetail.Rows[this.dgvDetail.CurrentCell.RowIndex]))
                                {
                                    return true;
                                }
                            }
                            else
                            {
                                MessageBox.Show(strErrMsg, SysVar.WarnTitle, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                                return true;
                            }
                        }
                        if (dgvDetail.CurrentCell.RowIndex < dgvDetail.Rows.Count - 1)
                        {
                            dgvDetail.CurrentCell = this.dgvDetail.Rows[dgvDetail.CurrentCell.RowIndex + 1].Cells[(int)DtlCol.WKName];
                        }
                        else
                        {
                            AddDtlRow(this.dgvDetail.Name);
                        }
                    }
                    else
                    {
                        if (CheckDtlRow(ref strErrMsg))
                        {
                            if (!Update_Detail(this.dgvDetail.Rows[this.dgvDetail.CurrentCell.RowIndex]))
                            {
                                return true;
                            }
                            AddDtlRow(this.dgvDetail.Name);
                        }
                        else
                        {
                            MessageBox.Show(strErrMsg, SysVar.WarnTitle, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                            return true;
                        }
                    }
                    return true;


                    #endregion
                case Keys.Up:
                    #region Key Up
                    if (CvtFunc.ToInt(dgvDetail.CurrentRow.Cells[(int)DtlCol.RecID].Value) > 0)
                    {
                        if (CvtFunc.ToInt(dgvDetail.CurrentRow.Cells[(int)DtlCol.ValueChanged].Value) > 0)
                        {
                            if (CheckDtlRow(ref strErrMsg))
                            {
                                if (!Update_Detail(this.dgvDetail.Rows[this.dgvDetail.CurrentCell.RowIndex])) return true;
                            }
                            else
                            {
                                MessageBox.Show(strErrMsg, SysVar.WarnTitle, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                                return true;
                            }
                        }
                        if (dgvDetail.CurrentCell.RowIndex > 0)
                        {
                            dgvDetail.CurrentCell = this.dgvDetail.Rows[dgvDetail.CurrentCell.RowIndex - 1].Cells[(int)DtlCol.WKName];
                        }
                    }
                    else
                    {
                        if (CheckDtlRow(ref strErrMsg))
                        {
                            if (!Update_Detail(this.dgvDetail.Rows[this.dgvDetail.CurrentCell.RowIndex])) return true;
                            if (dgvDetail.CurrentCell.RowIndex > 0)
                            {
                                dgvDetail.CurrentCell = this.dgvDetail.Rows[dgvDetail.CurrentCell.RowIndex - 1].Cells[(int)DtlCol.WKName];
                            }
                        }
                        else if (dgvDetail.Rows.Count > 1)
                        {

                            this.dgvDetail.Rows.Remove(this.dgvDetail.CurrentRow);
                            dgvDetail.CurrentCell = this.dgvDetail.Rows[dgvDetail.Rows.Count - 1].Cells[(int)DtlCol.WKName];
                        }
                    }
                    return true;
                    #endregion
                case Keys.Enter:
                    #region Key Enter
                    this.dgvDetail.EndEdit();
                    j = BaseFunctions.GetGridViewLastEnabledEditColumn(this.dgvDetail);
                    if (dgvDetail.CurrentCell.ColumnIndex == j)
                    {
                        if (CvtFunc.ToInt(dgvDetail.CurrentRow.Cells[(int)DtlCol.RecID].Value) > 0)
                        {
                            if (CvtFunc.ToInt(dgvDetail.CurrentRow.Cells[(int)DtlCol.ValueChanged].Value) > 0)
                            {
                                if (CheckDtlRow(ref strErrMsg))
                                {
                                    if (!Update_Detail(this.dgvDetail.Rows[this.dgvDetail.CurrentCell.RowIndex])) return true;
                                }
                                else
                                {
                                    MessageBox.Show(strErrMsg, SysVar.WarnTitle, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                                    return true;
                                }
                            }
                        }
                        else
                        {
                            if (CheckDtlRow(ref strErrMsg))
                            {
                                if (!Update_Detail(this.dgvDetail.Rows[this.dgvDetail.CurrentCell.RowIndex])) return true;
                            }
                            else
                            {
                                MessageBox.Show(strErrMsg, SysVar.WarnTitle, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                                return true;
                            }

                        }
                        if (dgvDetail.CurrentCell.RowIndex < dgvDetail.Rows.Count - 1)
                        {
                            dgvDetail.CurrentCell = this.dgvDetail.Rows[dgvDetail.CurrentCell.RowIndex + 1].Cells[(int)DtlCol.WKName];
                        }
                        else
                        {
                            AddDtlRow(this.dgvDetail.Name);
                        }
                    }
                    else
                    {
                        j = BaseFunctions.GetGridViewNextEnabledEditColumn(dgvDetail, dgvDetail.CurrentCell.ColumnIndex);
                        if (j >= 0) SendKeys.Send("{Tab}");
                        //  if (j >= 0) dgvDetail.CurrentCell = this.dgvDetail.Rows[dgvDetail.CurrentCell.RowIndex].Cells[j];
                    }

                    return true;
                    #endregion
            }
            return false;
        }
        private bool Update_Detail(DataGridViewRow dgvRow)
        {
            if (dgvRow == null) return false;
            string strSQL = string.Empty;
            this.dgvDetail.EndEdit(); 
            DBAccess dbcon = new DBAccess(false, false);
            string strFields = "SeqNo,CategoryID,CategoryName,WKProductID,WKName,WorkCostRate,WKPriceType,WKSTDQty,WKSalary,WKSTDSalary,STDQtyUnit,WKUnitCost,WKTotUnitCost";
            try
            {
                DataRow drDtl = dbcon.GetDataTable("select * from " + mstrTableName + " ").NewRow();
                if (CvtFunc.ToDouble(dgvRow.Cells[(int)DtlCol.RecID].Value) > 0)   //Update
                {
                    BaseFunctions.FillDataRowByGridRow(dgvRow, drDtl);
                    DataFunctions.UpdateTable(dbcon, strFields, mstrTableName , drDtl, FormStatus.Update);
                }
                else //Add New
                {
                    BaseFunctions.FillDataRowByGridRow(dgvRow, drDtl);
                    DataFunctions.UpdateTable(dbcon, strFields, mstrTableName, drDtl, FormStatus.AddNew);
                    dgvRow.Cells[(int)DtlCol.RecID].Value = CvtFunc.ToString(drDtl["RecID"]);
                }
                dbcon.CloseCon();
            }
            catch (Exception ex)
            {
                dbcon.CloseCon();
                return false;
            }
            return true;
        }
        private bool Delete_Detail(DataGridViewRow dgvRow)
        {
            if (dgvRow == null) return true;
            string strRecID = CvtFunc.ToString(dgvRow.Cells[(int)DtlCol.RecID].Value);
            string strSeqNo = CvtFunc.ToString(dgvRow.Cells[(int)DtlCol.RowSeqNo].Value);
            string strSQL = string.Empty;
            if (CvtFunc.ToDouble(strRecID) > 0)
            {
                if (MessageBox.Show("您确定要删除？", "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    DBAccess dbcon = new DBAccess(false, false);
                    try
                    {
                        dbcon.BeginTran();
                        strSQL = "Delete "+ mstrTableName +" where RecID='" + strRecID + "'";
                        dbcon.ExecuteSql(strSQL);

                        strSQL = "Delete " + mstrTableName_Sub + " where HdrRecID='" + strRecID + "'";
                        dbcon.ExecuteSql(strSQL);
                        dbcon.CommitTran();

                        //string strWKProductID = CvtFunc.ToString(this.dgvDetail.CurrentRow.Cells[(int)DtlCol.WKProductID].Value);
                        //string strWKPriceType = CvtFunc.ToString(this.dgvDetail.CurrentRow.Cells[(int)DtlCol.WKPriceType].Value);
                        //strSQL = "select * from imCategoryWKWorkCost where WKProductID='" + strWKProductID + "' and WKPriceType='" + strWKPriceType + "'";
                        //DataTable dtFind = dbcon.GetDataTable(strSQL);
                        //if (dtFind.Rows.Count > 0)
                        //{
                        //    if (MessageBox.Show("其他类别有相同的费用标准，是否也删除？", "", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes)
                        //    {
                        //        foreach (DataRow drRow in dtFind.Rows)
                        //        {
                        //            strSQL = "Delete " + mstrTableName + " where RecID='" + drRow["RecID"] + "'";
                        //            dbcon.ExecuteSql(strSQL);
                        //        }
                        //    }
                        //}
                    }
                    catch (Exception ex)
                    {
                        dbcon.RollbackTran();
                    }
                    dbcon.CloseCon();
                }
            }
            return true;
        }
        private void dgvDetail_CellLeave(object sender, DataGridViewCellEventArgs e)
        {
            this.dgvDetail.EndEdit();

            if (e.ColumnIndex == (int)DtlCol.WKName || e.ColumnIndex == (int)DtlCol.WorkCost )
            {
                string strErrMsg = string.Empty;
                if (CheckDtlRow(ref strErrMsg))
                {
                    Update_Detail(this.dgvDetail.Rows[e.RowIndex]);
                }
            }
            if (e.ColumnIndex == (int)DtlCol.WKName)
            {
                this.dgvDetail.Rows[e.RowIndex].Cells[(int)DtlCol.WKRemark].Value = DataFunctions.GetFieldValue("rmWorkingProcedure", "Remark", "WorkingProcedureName='" + CvtFunc.ToString(this.dgvDetail.CurrentRow.Cells[(int)DtlCol.WKName].Value) + "'");
            }
            else if (e.ColumnIndex == (int)DtlCol.WorkCost)
            {
                this.dgvDetail.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = BaseFunctions.FormatNumeric(this.dgvDetail.Rows[e.RowIndex].Cells[e.ColumnIndex].Value, "Ns.2");
            }
        }

        #endregion


        /// <summary>
        /// 相对应的岗位的GRID
        /// </summary>
        #region SubDetail 

        private void InitSubGridDtl()
        {

            this.dgvSubDetail.Columns.Clear();
            this.dgvSubDetail.Rows.Clear();

            DataGridViewTextBoxColumn colRowSeqNo = new DataGridViewTextBoxColumn();
            colRowSeqNo.HeaderText = "序号";
            colRowSeqNo.Visible = true;
            colRowSeqNo.Name = "RowSeqNo";
            colRowSeqNo.Width = 50;
            colRowSeqNo.ReadOnly = true;
            colRowSeqNo.SortMode = DataGridViewColumnSortMode.NotSortable;

            TextLookUpColumn colCategoryID = new TextLookUpColumn();
            colCategoryID.HeaderText = "类别编码";
            colCategoryID.Width = 70;
            colCategoryID.Name = "CategoryID";
            colCategoryID.ReadOnly = true;
            colCategoryID.Visible = true;

            TextLookUpColumn colCategoryName = new TextLookUpColumn();
            colCategoryName.HeaderText = "类别名称";
            colCategoryName.Width = 100;
            colCategoryName.Name = "CategoryName";
            colCategoryName.ReadOnly = true;
            colCategoryName.Visible = true;

            TextLookUpColumn colWKID = new TextLookUpColumn();
            colWKID.HeaderText = "工序编码";
            colWKID.Width = 70;
            colWKID.Name = "WKProductID";
            colWKID.ReadOnly = true;
            colWKID.SortMode = DataGridViewColumnSortMode.Automatic;

            TextLookUpColumn colWKName = new TextLookUpColumn();
            colWKName.HeaderText = "工序名称";
            colWKName.Width = 100;
            colWKName.Name = "WKName";
            colWKName.ReadOnly = true;
            colWKName.SortMode = DataGridViewColumnSortMode.Automatic;


            //WKPrictType,         //工价类别
            DataGridViewTextBoxColumn colWKPrictType = new DataGridViewTextBoxColumn();
            colWKPrictType.HeaderText = "工价类别";
            colWKPrictType.Width = 100;
            colWKPrictType.Name = "WKPriceType";
            colWKPrictType.ReadOnly = true;

             //WKPosition,         //岗位SeqNo
            DataGridViewTextBoxColumn colWorkPositionSeqNo = new DataGridViewTextBoxColumn();
            colWorkPositionSeqNo.HeaderText = "岗位";
            colWorkPositionSeqNo.Width = 100;
            colWorkPositionSeqNo.Name = "WorkPositionSeqNo";
            colWorkPositionSeqNo.ReadOnly = false;
            colWorkPositionSeqNo.Visible  = false ;

            //WKPosition,         //岗位
            TextLookUpColumn colWKPosition = new TextLookUpColumn();
            colWKPosition.HeaderText = "岗位";
            colWKPosition.Width = 100;
            colWKPosition.Name = "WKPosition";
            colWKPosition.ReadOnly = false ;

            //WKSTDQty,            //额定产能
            DataGridViewTextBoxColumn colWKSTDQty = new DataGridViewTextBoxColumn();
            colWKSTDQty.HeaderText = "额定产能";
            colWKSTDQty.Width = 100;
            colWKSTDQty.Name = "WKSTDQty";
            colWKSTDQty.ReadOnly = true;
            colWKSTDQty.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight;
            colWKSTDQty.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;

            //STDQtyUnit
            DataGridViewTextBoxColumn colSTDQtyUnit = new DataGridViewTextBoxColumn();
            colSTDQtyUnit.HeaderText = "单位";
            colSTDQtyUnit.Width = 50;
            colSTDQtyUnit.Name = "STDQtyUnit";
            colSTDQtyUnit.ReadOnly = false;
            colSTDQtyUnit.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight;
            colSTDQtyUnit.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;

            //WKSalary,            //固定工资
            DataGridViewTextBoxColumn colWKSalary = new DataGridViewTextBoxColumn();
            colWKSalary.HeaderText = "固定工资";
            colWKSalary.Width = 100;
            colWKSalary.Name = "WKSalary";
            colWKSalary.ReadOnly = false;
            colWKSalary.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight;
            colWKSalary.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;

            //WKSTDSalary,         //标准工资
            DataGridViewTextBoxColumn colWKSTDSalary = new DataGridViewTextBoxColumn();
            colWKSTDSalary.HeaderText = "标准工资";
            colWKSTDSalary.Width = 100;
            colWKSTDSalary.Name = "WKSTDSalary";
            colWKSTDSalary.ReadOnly = false;
            colWKSTDSalary.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight;
            colWKSTDSalary.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;

            //WKUnitCost     //计件工价
            DataGridViewTextBoxColumn colWKUnitCost = new DataGridViewTextBoxColumn();
            colWKUnitCost.HeaderText = "计件工价";
            colWKUnitCost.Width = 100;
            colWKUnitCost.Name = "WKUnitCost";
            colWKUnitCost.ReadOnly = false;
            colWKUnitCost.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight;
            colWKUnitCost.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;

            //WKTotUnitCost     //总工价
            DataGridViewTextBoxColumn colWKTotUnitCost = new DataGridViewTextBoxColumn();
            colWKTotUnitCost.HeaderText = "总工价";
            colWKTotUnitCost.Width = 100;
            colWKTotUnitCost.Name = "WKTotUnitCost";
            colWKTotUnitCost.ReadOnly = false;
            colWKTotUnitCost.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight;
            colWKTotUnitCost.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;


            DataGridViewTextBoxColumn colWKRemark = new DataGridViewTextBoxColumn();
            colWKRemark.HeaderText = "工序描述";
            colWKRemark.Width = 100;
            colWKRemark.Name = "WKRemark";
            colWKRemark.ReadOnly = true;

            DataGridViewTextBoxColumn colWorkCost = new DataGridViewTextBoxColumn();
            colWorkCost.HeaderText = "工时费用标准";
            colWorkCost.Width = 80;
            colWorkCost.Name = "WorkCostRate";
            colWorkCost.ReadOnly = false;
            colWorkCost.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight;
            colWorkCost.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;

            DataGridViewTextBoxColumn colWorkersQty = new DataGridViewTextBoxColumn();
            colWorkersQty.HeaderText = "定编人数";
            colWorkersQty.Width = 80;
            colWorkersQty.Name = "WorkersQty";
            colWorkersQty.ReadOnly = false;
            colWorkersQty.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight;
            colWorkersQty.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;


            DataGridViewTextBoxColumn colRecID = new DataGridViewTextBoxColumn();
            colRecID.HeaderText = "RecID";
            colRecID.Visible = false;
            colRecID.Name = "RecID";

            DataGridViewTextBoxColumn colValueChanged = new DataGridViewTextBoxColumn();
            colValueChanged.HeaderText = "ValueChanged";
            colValueChanged.Visible = false;
            colValueChanged.Name = "ValueChanged";

            DataGridViewImageColumn colDel = new DataGridViewImageColumn();
            Bitmap delbitmap = new Bitmap(this.iglSource.Images["del.ico"]);
            colDel.Width = 20;
            colDel.Name = "DelLine";
            colDel.HeaderText = "";
            colDel.Resizable = DataGridViewTriState.False;

            DataGridViewTextBoxColumn colHdrRecID = new DataGridViewTextBoxColumn();
            colHdrRecID.HeaderText = "HdrRecID";
            colHdrRecID.Visible = false;
            colHdrRecID.Name = "HdrRecID";


            this.dgvSubDetail.Columns.AddRange(new DataGridViewColumn[] { colRowSeqNo, colCategoryID, colCategoryName, colWKID, colWKName, colWKPrictType, colWorkPositionSeqNo, colWKPosition, colWKSTDQty, colSTDQtyUnit, colWKSalary, colWKSTDSalary, colWKUnitCost, colWKTotUnitCost, colWKRemark, colWorkCost, colWorkersQty, colRecID, colValueChanged, colDel, colHdrRecID });

            mDtlColumnReadOnlyArray = new bool[dgvSubDetail.Columns.Count];
            string[] ColumnWidthArray = DataFunctions.GetGridColumnWidthString(this.Name, this.CurrentMenuInfo.iParameter, SysVar.UserName, this.Name + "." + this.dgvSubDetail.Name).Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
            for (int i = 0; i < dgvSubDetail.Columns.Count; i++)
            {
                mDtlColumnReadOnlyArray[i] = dgvSubDetail.Columns[i].ReadOnly;
                if (ColumnWidthArray.Length == dgvSubDetail.Columns.Count) dgvSubDetail.Columns[i].Width = CvtFunc.ToInt(ColumnWidthArray[i]);
            }

            this.dgvSubDetail.EditMode = DataGridViewEditMode.EditOnEnter;
            this.dgvSubDetail.RowTemplate.Height = 20;
            this.dgvSubDetail.RowHeadersWidth = 23;
            this.dgvSubDetail.RowHeadersBorderStyle = DataGridViewHeaderBorderStyle.None;
            this.dgvSubDetail.RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.DisableResizing;
            this.dgvSubDetail.ColumnHeadersHeight = 30;
            this.dgvSubDetail.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing;
            this.dgvSubDetail.SelectionMode = DataGridViewSelectionMode.CellSelect;
            this.dgvSubDetail.AllowUserToResizeRows = false;

        }

        private void FillSubDtl()
        {
            FillSubDtl(-1);
        }

        private void FillSubDtl(int intRow)
        {
            if ( intRow >0 && this.dgvDetail.Rows[intRow] == null) return;
            string strSql = "";
            string strUserName = "";
            string strRecid = "";
            bool bAccess = false;
            m_bCanTrigerCellValueChangedEven = false;

            this.dgvSubDetail.Rows.Clear();
            DBAccess dbCon = new DBAccess(false, false);

            if (intRow > -1)
            {
                string strHdrRecID = CvtFunc.ToString(this.dgvDetail.Rows[intRow].Cells[(int)DtlCol.RecID].Value);
                if (strHdrRecID == "") return;
                strSql = " select A.*,B.WorkingProcedureName,b.Remark from " + mstrTableName_Sub + " A inner join rmWorkingProcedure B on A.WKProductID=B.WorkingProcedureID " +
                        " where  HdrRecID  = " + strHdrRecID + " order by A.CategoryID, A.WKProductID";
            }
            else
            {
                strSql = " select A.*,B.WorkingProcedureName,b.Remark from " + mstrTableName_Sub + " A inner join rmWorkingProcedure B on A.WKProductID=B.WorkingProcedureID " +
                         " order by A.CategoryID, A.WKProductID";
            }
            DataTable mUserAlertTable = dbCon.GetDataSet(strSql).Tables[0];

            for (int i = 0; i < mUserAlertTable.Rows.Count; i++)
            {
                DataRow dr = mUserAlertTable.Rows[i];
                if (this.dgvSubDetail.Rows.Count <= i) this.dgvSubDetail.Rows.Add();
                dgvSubDetail.Rows[dgvSubDetail.Rows.Count - 1].Cells[(int)DtlCol.RowSeqNo].Value = dgvSubDetail.Rows.Count.ToString();
                dgvSubDetail.Rows[dgvSubDetail.Rows.Count - 1].Cells[(int)DtlCol.WKProductID].Value = dr["WKProductID"].ToString().TrimEnd();
                dgvSubDetail.Rows[dgvSubDetail.Rows.Count - 1].Cells[(int)DtlCol.WKName].Value = dr["WorkingProcedureName"].ToString().TrimEnd();
                dgvSubDetail.Rows[dgvSubDetail.Rows.Count - 1].Cells[(int)DtlCol.WKRemark].Value = dr["Remark"].ToString().TrimEnd();
                dgvSubDetail.Rows[dgvSubDetail.Rows.Count - 1].Cells[(int)DtlCol.CategoryID].Value = dr["CategoryID"].ToString().TrimEnd();
                dgvSubDetail.Rows[dgvSubDetail.Rows.Count - 1].Cells[(int)DtlCol.CategoryName].Value = dr["CategoryName"].ToString().TrimEnd();

                dgvSubDetail.Rows[dgvSubDetail.Rows.Count - 1].Cells[(int)DtlCol.WKPriceType].Value = CvtFunc.ToString(dr["WKPriceType"].ToString());
                dgvSubDetail.Rows[dgvSubDetail.Rows.Count - 1].Cells[(int)DtlCol.WorkPositionSeqNo].Value = CvtFunc.ToString(dr["WorkPositionSeqNo"].ToString());
                dgvSubDetail.Rows[dgvSubDetail.Rows.Count - 1].Cells[(int)DtlCol.WKPostiton].Value = dbCon.GetValue("select WorkPositionName From rmWorkPosition where WorkPositionSeqNo='" + dr["WorkPositionSeqNo"] + "'");

                dgvSubDetail.Rows[dgvSubDetail.Rows.Count - 1].Cells[(int)DtlCol.WKSTDQty].Value = BaseFunctions.FormatNumeric(dr["WKSTDQty"].ToString(), "Ns.2");
                dgvSubDetail.Rows[dgvSubDetail.Rows.Count - 1].Cells[(int)DtlCol.STDQtyUnit].Value = CvtFunc.ToString(dr["STDQtyUnit"].ToString());


                dgvSubDetail.Rows[dgvSubDetail.Rows.Count - 1].Cells[(int)DtlCol.WKSalary].Value = BaseFunctions.FormatNumeric(dr["WKSalary"].ToString(), "Ns.2");
                dgvSubDetail.Rows[dgvSubDetail.Rows.Count - 1].Cells[(int)DtlCol.WKSTDSalary].Value = BaseFunctions.FormatNumeric(dr["WKSTDSalary"].ToString(), "Ns.2");
                dgvSubDetail.Rows[dgvSubDetail.Rows.Count - 1].Cells[(int)DtlCol.WKUnitCost].Value = BaseFunctions.FormatNumeric(dr["WKUnitCost"].ToString(), "Ns.6");
                dgvSubDetail.Rows[dgvSubDetail.Rows.Count - 1].Cells[(int)DtlCol.WKTotUnitCost].Value = BaseFunctions.FormatNumeric(dr["WKTotUnitCost"].ToString(), "Ns.6");

                dgvSubDetail.Rows[dgvSubDetail.Rows.Count - 1].Cells[(int)DtlCol.WorkCost].Value = BaseFunctions.FormatNumeric(dr["WorkCostRate"], "ns.2");

                dgvSubDetail.Rows[dgvSubDetail.Rows.Count - 1].Cells[(int)DtlCol.WorkersQty].Value = BaseFunctions.FormatNumeric(dr["WorkersQty"], "ns.0");
                dgvSubDetail.Rows[dgvSubDetail.Rows.Count - 1].Cells[(int)DtlCol.RecID].Value = CvtFunc.ToInt(dr["RecID"]);
                dgvSubDetail.Rows[dgvSubDetail.Rows.Count - 1].Cells[(int)DtlCol.HdrRecID].Value = CvtFunc.ToInt(dr["HdrRecID"]);
                
                dgvSubDetail.Rows[dgvSubDetail.Rows.Count - 1].Cells[(int)DtlCol.ValueChanged].Value = "0";
            }
            if (dgvSubDetail.Rows.Count == 0)
            {
                AddSubDtlRow(this.dgvSubDetail.Name,intRow);
                dgvSubDetail_CellValueChanged(this.dgvSubDetail, new DataGridViewCellEventArgs((int)DtlCol.WKSalary, dgvSubDetail.Rows.Count - 1));
                this.dgvSubDetail.CurrentCell = this.dgvSubDetail.CurrentRow.Cells[(int)DtlCol.WKPostiton];  
                dgvSubDetail.Rows[dgvSubDetail.Rows.Count - 1].Cells[(int)DtlCol.ValueChanged].Value = "1";
            }
            dbCon.CloseCon();
            base.SetBlankImage(dgvSubDetail);
            m_bCanTrigerCellValueChangedEven = true;
        }
        private void dgvSubDetail_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

            if (dgvSubDetail.Columns[e.ColumnIndex] is TextLookUpColumn)
            {
                if ((dgvSubDetail.Columns[e.ColumnIndex] as TextLookUpColumn).CanLookUp())
                {
                    SubDtl_LookUp(e.ColumnIndex);
                }
            }
            else
            {
                if (this.dgvSubDetail.CurrentCell.ColumnIndex == this.dgvSubDetail.Columns["DelLine"].Index && base.HasDelImage(this.dgvSubDetail, e))
                {
                    this.dgvSubDetail.CurrentCell.Selected = true;
                    this.dgvSubDetail.CurrentCell.Selected = false;
                    if (!this.Delete_SubDetail(this.dgvSubDetail.Rows[this.dgvSubDetail.CurrentCell.RowIndex]))
                    {
                        return;
                    }
                    this.dgvSubDetail.Rows.Remove(this.dgvSubDetail.Rows[this.dgvSubDetail.CurrentCell.RowIndex]);
                    if (this.dgvSubDetail.Rows.Count == 0)
                    {
                        this.AddSubDtlRow(this.dgvSubDetail.Name);
                    }
                }
            }
        }
        private void AddSubDtlRow(string strGridName)
        {
            AddSubDtlRow(strGridName, 0);
        }

        private void AddSubDtlRow(string strGridName, int iRow)
        {
            if (mblnInDetailMode) return;
            if (this.dgvDetail.CurrentRow == null) return;
            if (CvtFunc.ToDouble(this.dgvDetail.CurrentRow.Cells[(int)DtlCol.RecID].Value) <= 0)
            {
                return;
            }
            DataGridViewRow dgvRow = this.dgvDetail.Rows[iRow] ; //获取HDR表的当前行
            if (strGridName == dgvSubDetail.Name)
            {
                int intRow = this.dgvSubDetail.Rows.Count ;
                m_bCanTrigerCellValueChangedEven = false;
                if (iRow >= 0)
                {                    
                    this.dgvSubDetail.Rows.Insert(intRow, 1);
                    
                }
                else
                {
                    this.dgvSubDetail.Rows.Add();
                    intRow = this.dgvSubDetail.Rows.Count - 1;
                }

                dgvSubDetail.Rows[intRow].Cells[(int)DtlCol.RowSeqNo].Value = dgvSubDetail.Rows.Count.ToString();
                dgvSubDetail.Rows[intRow].Cells[(int)DtlCol.CategoryID].Value = dgvDetail.Rows[iRow].Cells["CategoryID"].Value   ;
                dgvSubDetail.Rows[intRow].Cells[(int)DtlCol.CategoryName].Value = DataFunctions.GetFieldValue("rmCategory", "CategoryName", "CategoryID='" + dgvDetail.Rows[iRow].Cells["CategoryID"].Value + "'");
                dgvSubDetail.Rows[intRow].Cells[(int)DtlCol.WKProductID].Value = dgvRow.Cells[(int)DtlCol.WKProductID].Value;
                dgvSubDetail.Rows[intRow].Cells[(int)DtlCol.WKName].Value = dgvRow.Cells[(int)DtlCol.WKName].Value;

                dgvSubDetail.Rows[intRow].Cells[(int)DtlCol.WKRemark].Value = "";                
                //dgvSubDetail.Rows[intRow].Cells[(int)DtlCol.WKName].Value = DataFunctions.GetFieldValue("rmWorkingProcedure", "WorkingProcedureName", "WorkingProcedureID='" + mstrWKProductID + "'");

                dgvSubDetail.Rows[intRow].Cells[(int)DtlCol.WKPriceType].Value = dgvRow.Cells[(int)DtlCol.WKPriceType].Value     ;
                if (this.dgvSubDetail.Rows.Count < 1)
                {
                    dgvSubDetail.Rows[intRow].Cells[(int)DtlCol.WKSalary].Value = dgvRow.Cells[(int)DtlCol.WKSalary].Value;
                    dgvSubDetail.Rows[intRow].Cells[(int)DtlCol.WKSTDSalary].Value = dgvRow.Cells[(int)DtlCol.WKSTDSalary].Value;
                }
                dgvSubDetail.Rows[intRow].Cells[(int)DtlCol.WKSTDQty].Value = dgvRow.Cells[(int)DtlCol.WKSTDQty].Value;                
                dgvSubDetail.Rows[intRow].Cells[(int)DtlCol.STDQtyUnit].Value = dgvRow.Cells[(int)DtlCol.STDQtyUnit].Value; ;
                 
                
                dgvSubDetail.Rows[intRow].Cells[(int)DtlCol.WKUnitCost].Value = "0.000000";
                dgvSubDetail.Rows[intRow].Cells[(int)DtlCol.WKTotUnitCost].Value = "0.000000";
                dgvSubDetail.Rows[intRow].Cells[(int)DtlCol.RecID].Value = -1;
                dgvSubDetail.Rows[intRow].Cells[(int)DtlCol.ValueChanged].Value = "0";
                dgvSubDetail.Rows[intRow].Cells[(int)DtlCol.WorkersQty].Value = "1";  
                dgvSubDetail.Rows[intRow].Cells[(int)DtlCol.HdrRecID].Value = dgvRow.Cells[(int)DtlCol.RecID].Value;      
                
                dgvSubDetail.CurrentCell = dgvSubDetail.Rows[intRow].Cells[(int)DtlCol.CategoryID];
                m_bCanTrigerCellValueChangedEven = true;
            }
        }
        private void SubDtl_LookUp(int iColumnIndex)
        {
            string strSQL = "", strCaption = "", strFindValue = "", strFindField = "";
            switch ((DtlCol)iColumnIndex)
            {
                case DtlCol.WKProductID:
                    strSQL = WP.Commons.BaseFunctions.GetLookUpSQL("rmWorkingProcedure");
                    strCaption = "工序选择";
                    if (this.dgvSubDetail.CurrentRow.Cells[(int)DtlCol.WKName].Value != null) strFindValue = this.dgvSubDetail.CurrentRow.Cells[(int)DtlCol.WKName].Value.ToString();
                    strFindField = "WorkingProcedureID";
                    break;
                case DtlCol.CategoryID:
                    strSQL = WP.Commons.BaseFunctions.GetLookUpSQL("rmCategory");
                    strCaption = "类别选择";
                    if (this.dgvSubDetail.CurrentRow.Cells[(int)DtlCol.CategoryID].Value != null) strFindValue = this.dgvSubDetail.CurrentRow.Cells[(int)DtlCol.CategoryID].Value.ToString();
                    strFindField = "CategoryID";
                    break;
                case DtlCol.WKPostiton :
                    strSQL = WP.Commons.BaseFunctions.GetLookUpSQL("rmWorkPosition");
                    strCaption = "岗位选择";
                    if (this.dgvSubDetail.CurrentRow.Cells[(int)DtlCol.WKPostiton].Value != null) strFindValue = this.dgvSubDetail.CurrentRow.Cells[(int)DtlCol.WKPostiton].Value.ToString();
                    strFindField = "WorkPositionSeqNo";
                    break;

            }

            if (strSQL == "") return;
            if (mblnLookUpSortByDescription)
            {
                strSQL = DataFunctions.GetSqlByDescriptionSort(strSQL);
                mblnLookUpSortByDescription = false;
            }

            frmLookUp frmlookup = new frmLookUp(strSQL, strCaption, strFindValue, strFindField);
            frmlookup.ShowDialog();

            if (frmlookup.HaveReturnValue)
            {
                switch ((DtlCol)iColumnIndex)
                {
                    case DtlCol.WKProductID:
                        this.dgvSubDetail.CurrentRow.Cells[(int)DtlCol.WKProductID].Value = frmlookup.GetReturnValue("WorkingProcedureID");
                        this.dgvSubDetail.CurrentRow.Cells[(int)DtlCol.WKName].Value = frmlookup.GetReturnValue("WorkingProcedureName");
                        this.dgvSubDetail.CurrentRow.Cells[(int)DtlCol.WKRemark].Value = frmlookup.GetReturnValue("Remark");
                        break;
                    case DtlCol.CategoryID:
                        this.dgvSubDetail.CurrentRow.Cells[(int)DtlCol.CategoryID].Value = frmlookup.GetReturnValue("CategoryID");
                        this.dgvSubDetail.CurrentRow.Cells[(int)DtlCol.CategoryName].Value = frmlookup.GetReturnValue("CategoryName");
                        break;
                    case DtlCol.WKPostiton:
                        this.dgvSubDetail.CurrentRow.Cells[(int)DtlCol.WorkPositionSeqNo].Value = frmlookup.GetReturnValue("WorkPositionSeqNo");
                        this.dgvSubDetail.CurrentRow.Cells[(int)DtlCol.WKPostiton].Value = frmlookup.GetReturnValue("WorkPositionName");
                        break;
                }
            }
            frmlookup.Close();
        }
        private bool CheckSubDtlRow(ref string strErrMsg)
        {
            bool bCheckResult = true;
            for (int i = 0; i < dgvSubDetail.ColumnCount - 1; i++)
            {

                bCheckResult = CheckSubDtlCell(i, ref strErrMsg);
                if (!bCheckResult)
                {
                    return false;
                    break;
                }
            }
            string strRecID = CvtFunc.ToString(this.dgvSubDetail.CurrentRow.Cells[(int)DtlCol.RecID].Value);
            string strCategoryName = CvtFunc.ToString(this.dgvSubDetail.CurrentRow.Cells[(int)DtlCol.CategoryID].Value);
            string strWKName = CvtFunc.ToString(this.dgvSubDetail.CurrentRow.Cells[(int)DtlCol.WKProductID].Value);
            string strWKPriceType = CvtFunc.ToString(this.dgvSubDetail.CurrentRow.Cells[(int)DtlCol.WKPriceType].Value);
            string strWorkPosition = CvtFunc.ToString(this.dgvSubDetail.CurrentRow.Cells[(int)DtlCol.WorkPositionSeqNo].Value);
            string strSQL = "select * from " + mstrTableName_Sub + " where RecID!='" + strRecID + "' and CategoryID='" + strCategoryName + "' and WKProductID='" + strWKName + "' and WKPriceType='" + strWKPriceType + "' and WorkPositionSeqNo='"+ strWorkPosition +"'";
            DataRow drCheck = mdbcon.GetDataFirstRow(strSQL);
            if (drCheck != null)
            {
                strErrMsg = "此设置已经存在，不能重复添加。";
                return false;
            }
            return bCheckResult;
        }
        private bool CheckSubDtlCell(int iColumn, ref string strErrMsg)
        {
            bool bReturnValue = true;
            strErrMsg = "";
            string strValue = "";
            if (this.dgvSubDetail.CurrentRow != null && this.dgvSubDetail.CurrentRow.Cells[iColumn].Value != null)
            {
                strValue = this.dgvSubDetail.CurrentRow.Cells[iColumn].Value.ToString().TrimEnd();
            }
            switch ((DtlCol)iColumn)
            {
                case DtlCol.CategoryName:
                    if (strValue.Trim() == "")
                    {
                        strErrMsg = "类别名称不能空.";
                        return false;
                    }
                    if (!DataFunctions.CheckRecordExist("rmCategory", strValue, "CategoryName"))
                    {
                        strErrMsg = "不存在的类别名称";
                        return false;
                    }
                    return true;
                case DtlCol.WKProductID:
                    if (strValue.Trim() == "")
                    {
                        strErrMsg = "工序不能空.";
                        return false;
                    }
                    if (!DataFunctions.CheckRecordExist("rmWorkingProcedure", strValue, "WorkingProcedureID"))
                    {
                        strErrMsg = "不存在的工序";
                        return false;
                    }
                    return true;
                case DtlCol.WKName:
                    if (strValue.Trim() == "")
                    {
                        strErrMsg = "工序名称不能空.";
                        return false;
                    }
                    if (!DataFunctions.CheckRecordExist("rmWorkingProcedure", strValue, "WorkingProcedureName"))
                    {
                        strErrMsg = "不存在的工序名称";
                        return false;
                    }
                    return true;
                case DtlCol.WorkPositionSeqNo:
                    if (strValue == "")
                    {
                        strErrMsg = "岗位不能为空";
                        return false;
                    }
                    else if (!DataFunctions.CheckRecordExist("rmWorkPosition", strValue, "WorkPositionSeqNo"))
                    {
                        strErrMsg = "不存在的岗位名称";
                        return false;
                    }
                    break;
                case DtlCol.WKPostiton :
                    if (strValue == "")
                    {
                        strErrMsg = "岗位不能为空";
                        return false;
                    }
                    else if (!DataFunctions.CheckRecordExist("rmWorkPosition", strValue, "WorkPositionName"))
                    {
                        strErrMsg = "不存在的岗位名称";
                        return false;
                    }
                    break;
            }
            return bReturnValue;
        }
        private void dgvSubDetail_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
        {
            if (dgvSubDetail.CurrentRow == null)
            {
                e.Cancel = false;
                return;
            }
            if (CvtFunc.ToInt(dgvSubDetail.CurrentRow.Cells[(int)DtlCol.RecID].Value) > 0 && CvtFunc.ToInt(dgvSubDetail.CurrentRow.Cells[(int)DtlCol.ValueChanged].Value) == 0)
            {
                e.Cancel = false;
                return;

            }

            if (mhtiDtl != null && mhtiDtl.ColumnIndex == (int)DtlCol.DelButton)
            {
                mhtiDtl = null;
                return;
            }


            if (this.dgvSubDetail.CurrentCell.ColumnIndex == (int)DtlCol.DelButton)
            {
                e.Cancel = false;
                return;

            }
            string strErrMsg = "";
            if (!BaseFunctions.NeedValidGridCell(this.ActiveControl, "dgvSubDetail") || m_bSettingStatus || !m_bCanTrigerCellValueChangedEven)
            {
                e.Cancel = false;
                return;
            }

            e.Cancel = !CheckSubDtlCell(e.ColumnIndex, ref strErrMsg);
            if (mPressExit) { e.Cancel = false; return; };
            if (strErrMsg != "")
            {
                MessageBox.Show(strErrMsg, SysVar.WarnTitle, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }
        private void dgvSubDetail_RowValidating(object sender, DataGridViewCellCancelEventArgs e)
        {
            if (dgvSubDetail.CurrentRow != null && CvtFunc.ToInt(dgvSubDetail.CurrentRow.Cells[(int)DtlCol.RecID].Value) > 0 && CvtFunc.ToInt(dgvSubDetail.CurrentRow.Cells[(int)DtlCol.ValueChanged].Value) == 0)
            {
                e.Cancel = false;
                return;

            }

            string strErrMsg = "";
            if (!BaseFunctions.NeedValidGridCell(this.ActiveControl, "dgvSubDetail") || m_bSettingStatus || !m_bCanTrigerCellValueChangedEven)
            {
                e.Cancel = false;
                return;
            }
            e.Cancel = !CheckDtlRow(ref strErrMsg);
            if (mPressExit) { e.Cancel = false; return; };
            if (strErrMsg != "")
            {
                MessageBox.Show(strErrMsg, SysVar.WarnTitle, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }

        }
        private void dgvSubDetail_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            if (!m_bCanTrigerCellValueChangedEven || e.ColumnIndex == (int)DtlCol.ValueChanged || e.ColumnIndex == (int)DtlCol.DelButton) return;
            dgvSubDetail.Rows[e.RowIndex].Cells[(int)DtlCol.ValueChanged].Value = "1";
            m_bDtlChanged = true;
            dgvSubDetail.EndEdit();
            if (e.ColumnIndex == (int)DtlCol.CategoryID)
            {
                string strCategoryID = CvtFunc.ToString(this.dgvSubDetail.CurrentRow.Cells[(int)DtlCol.CategoryID].Value);
                this.dgvSubDetail.CurrentRow.Cells[(int)DtlCol.CategoryName].Value = DataFunctions.GetFieldValue("rmCategory", "CategoryName", "CategoryID='" + strCategoryID + "'");
            }
            if (e.ColumnIndex == (int)DtlCol.WKProductID)
            {
                string strWKName = CvtFunc.ToString(this.dgvSubDetail.CurrentRow.Cells[(int)DtlCol.WKProductID].Value);
                this.dgvSubDetail.CurrentRow.Cells[(int)DtlCol.WKName].Value = DataFunctions.GetFieldValue("rmWorkingProcedure", "WorkingProcedureName", "WorkingProcedureID='" + strWKName + "'");
                this.dgvSubDetail.CurrentRow.Cells[(int)DtlCol.WKRemark].Value = DataFunctions.GetFieldValue("rmWorkingProcedure", "Remark", "WorkingProcedureID='" + strWKName + "'");
            }
            if (e.ColumnIndex == (int)DtlCol.WKSalary || e.ColumnIndex == (int)DtlCol.WKSTDQty || e.ColumnIndex == (int)DtlCol.WKSTDSalary)
            {
                //计件工资：
                //(标准工资-固定工资/22*26）/额定产量/26
                //(dblStandardWage26 - dblStandardWage / 22d * 26) / dblStandardQty / 26d;

                //总工价：
                //标准工资是26天的
                //日工资=标准工资/26天
                //总工价=日工资/额定产能

                double dblStandardWage26 = CvtFunc.ToDouble(this.dgvSubDetail.CurrentRow.Cells[(int)DtlCol.WKSTDSalary].Value);
                double dblStandardWage = CvtFunc.ToDouble(this.dgvSubDetail.CurrentRow.Cells[(int)DtlCol.WKSalary].Value);
                double dblStandardQty = CvtFunc.ToDouble(this.dgvSubDetail.CurrentRow.Cells[(int)DtlCol.WKSTDQty].Value);
                if (dblStandardQty == 0)
                {
                    this.dgvSubDetail.CurrentRow.Cells[(int)DtlCol.WKUnitCost].Value = "0";
                    this.dgvSubDetail.CurrentRow.Cells[(int)DtlCol.WKTotUnitCost].Value = "0";
                }
                else
                {
                    this.dgvSubDetail.CurrentRow.Cells[(int)DtlCol.WKUnitCost].Value = BaseFunctions.FormatNumeric((dblStandardWage26 - dblStandardWage / 22d * 26) / dblStandardQty / 26d, "ns.6");
                    this.dgvSubDetail.CurrentRow.Cells[(int)DtlCol.WKTotUnitCost].Value = BaseFunctions.FormatNumeric(dblStandardWage26 / 26d / dblStandardQty, "ns.6");
                }
                if (m_bCanTrigerCellValueChangedEven)
                {
                    Update_SubDetail(this.dgvSubDetail.CurrentRow);
                }
            }
            dgvSubDetail.EndEdit();
        }
        private void dgvSubDetail_CellEnter(object sender, DataGridViewCellEventArgs e)
        {
            if (this.dgvSubDetail.CurrentCell.ColumnIndex == (int)DtlCol.DelButton)
            {
                this.dgvSubDetail.CurrentCell.Selected = false;
            }
            if (CvtFunc.ToDouble(this.dgvSubDetail.Rows[this.dgvSubDetail.CurrentCell.RowIndex].Cells[(int)DtlCol.RecID].Value) > 0)
            {
                this.dgvSubDetail.Rows[this.dgvSubDetail.CurrentCell.RowIndex].Cells[(int)DtlCol.CategoryName].ReadOnly = true;
            }
        }
        private void dgvSubDetail_CellContentDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (this.dgvSubDetail.CurrentCell.ColumnIndex == (int)DtlCol.DelButton)
            {
                this.dgvSubDetail.CurrentCell.Selected = false;
            }
        }
        private void dgvSubDetail_DataError(object sender, DataGridViewDataErrorEventArgs e)
        {
            return;
        }
        private void dgvSubDetail_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            base.SetGridViewDelImage(this.dgvSubDetail, e, (int)DtlCol.DelButton);
            string strErrMsg = "";
            if (dgvSubDetail.CurrentRow != null && CvtFunc.ToInt(dgvSubDetail.CurrentRow.Cells[(int)DtlCol.ValueChanged].Value) > 0)
            {
                if (CheckSubDtlRow(ref strErrMsg))
                {
                    if (!Update_SubDetail(this.dgvSubDetail.Rows[this.dgvSubDetail.CurrentCell.RowIndex]))
                    {
                        return;
                    }

                    //AddDtlRow(this.dgvSubDetail.Name);
                }
                else
                {
                    return;
                }
            }
        }
        private void dgvSubDetail_MouseDown(object sender, MouseEventArgs e)
        {

            mhtiDtl = this.dgvSubDetail.HitTest(e.X, e.Y);

            //System.Windows.Forms.DataGridView.HitTestInfo hti;
            //hti = this.dgvSubDetail.HitTest(e.X, e.Y);
            //switch (hti.Type)
            //{
            //    case System.Windows.Forms.DataGridViewHitTestType.Cell:
            //        break;
            //}
        }
        private bool dgvSubDetail_KeyDownEvent(System.Windows.Forms.Keys keyData)
        {
            string strErrMsg = "";
            int j;
            switch (keyData)
            {
                case Keys.Down:
                    #region Key Down

                    this.dgvSubDetail.EndEdit();
                    if (CvtFunc.ToInt(dgvSubDetail.CurrentRow.Cells[(int)DtlCol.RecID].Value) > 0)
                    {
                        if (CvtFunc.ToInt(dgvSubDetail.CurrentRow.Cells[(int)DtlCol.ValueChanged].Value) > 0)
                        {

                            if (CheckSubDtlRow(ref strErrMsg))
                            {

                                if (!Update_SubDetail(this.dgvSubDetail.Rows[this.dgvSubDetail.CurrentCell.RowIndex]))
                                {
                                    return true;
                                }
                            }
                            else
                            {
                                MessageBox.Show(strErrMsg, SysVar.WarnTitle, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                                return true;
                            }
                        }
                        if (dgvSubDetail.CurrentCell.RowIndex < dgvSubDetail.Rows.Count - 1)
                        {
                            dgvSubDetail.CurrentCell = this.dgvSubDetail.Rows[dgvSubDetail.CurrentCell.RowIndex + 1].Cells[(int)DtlCol.WKName];
                        }
                        else
                        {
                            AddSubDtlRow(this.dgvSubDetail.Name,this.dgvDetail.CurrentRow.Index);
                        }
                    }
                    else
                    {
                        if (CheckSubDtlRow(ref strErrMsg))
                        {
                            if (!Update_SubDetail(this.dgvSubDetail.Rows[this.dgvSubDetail.CurrentCell.RowIndex]))
                            {
                                return true;
                            }
                            AddSubDtlRow(this.dgvSubDetail.Name);
                        }
                        else
                        {
                            MessageBox.Show(strErrMsg, SysVar.WarnTitle, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                            return true;
                        }
                    }
                    return true;


                    #endregion
                case Keys.Up:
                    #region Key Up
                    if (CvtFunc.ToInt(dgvSubDetail.CurrentRow.Cells[(int)DtlCol.RecID].Value) > 0)
                    {
                        if (CvtFunc.ToInt(dgvSubDetail.CurrentRow.Cells[(int)DtlCol.ValueChanged].Value) > 0)
                        {
                            if (CheckSubDtlRow(ref strErrMsg))
                            {
                                if (!Update_SubDetail(this.dgvSubDetail.Rows[this.dgvSubDetail.CurrentCell.RowIndex])) return true;
                            }
                            else
                            {
                                MessageBox.Show(strErrMsg, SysVar.WarnTitle, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                                return true;
                            }
                        }
                        if (dgvSubDetail.CurrentCell.RowIndex > 0)
                        {
                            dgvSubDetail.CurrentCell = this.dgvSubDetail.Rows[dgvSubDetail.CurrentCell.RowIndex - 1].Cells[(int)DtlCol.WKName];
                        }
                    }
                    else
                    {
                        if (CheckSubDtlRow(ref strErrMsg))
                        {
                            if (!Update_SubDetail(this.dgvSubDetail.Rows[this.dgvSubDetail.CurrentCell.RowIndex])) return true;
                            if (dgvSubDetail.CurrentCell.RowIndex > 0)
                            {
                                dgvSubDetail.CurrentCell = this.dgvSubDetail.Rows[dgvSubDetail.CurrentCell.RowIndex - 1].Cells[(int)DtlCol.WKName];
                            }
                        }
                        else if (dgvSubDetail.Rows.Count > 1)
                        {

                            this.dgvSubDetail.Rows.Remove(this.dgvSubDetail.CurrentRow);
                            dgvSubDetail.CurrentCell = this.dgvSubDetail.Rows[dgvSubDetail.Rows.Count - 1].Cells[(int)DtlCol.WKName];
                        }
                    }
                    return true;
                    #endregion
                case Keys.Enter:
                    #region Key Enter
                    this.dgvSubDetail.EndEdit();
                    j = BaseFunctions.GetGridViewLastEnabledEditColumn(this.dgvSubDetail);
                    if (dgvSubDetail.CurrentCell.ColumnIndex == j)
                    {
                        if (CvtFunc.ToInt(dgvSubDetail.CurrentRow.Cells[(int)DtlCol.RecID].Value) > 0)
                        {
                            if (CvtFunc.ToInt(dgvSubDetail.CurrentRow.Cells[(int)DtlCol.ValueChanged].Value) > 0)
                            {
                                if (CheckSubDtlRow(ref strErrMsg))
                                {
                                    if (!Update_SubDetail(this.dgvSubDetail.Rows[this.dgvSubDetail.CurrentCell.RowIndex])) return true;
                                }
                                else
                                {
                                    MessageBox.Show(strErrMsg, SysVar.WarnTitle, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                                    return true;
                                }
                            }
                        }
                        else
                        {
                            if (CheckSubDtlRow(ref strErrMsg))
                            {
                                if (!Update_SubDetail(this.dgvSubDetail.Rows[this.dgvSubDetail.CurrentCell.RowIndex])) return true;
                            }
                            else
                            {
                                MessageBox.Show(strErrMsg, SysVar.WarnTitle, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                                return true;
                            }

                        }
                        if (dgvSubDetail.CurrentCell.RowIndex < dgvSubDetail.Rows.Count - 1)
                        {
                            dgvSubDetail.CurrentCell = this.dgvSubDetail.Rows[dgvSubDetail.CurrentCell.RowIndex + 1].Cells[(int)DtlCol.WKName];
                        }
                        else
                        {
                            AddDtlRow(this.dgvSubDetail.Name);
                        }
                    }
                    else
                    {
                        j = BaseFunctions.GetGridViewNextEnabledEditColumn(dgvSubDetail, dgvSubDetail.CurrentCell.ColumnIndex);
                        if (j >= 0) SendKeys.Send("{Tab}");
                        //  if (j >= 0) dgvSubDetail.CurrentCell = this.dgvSubDetail.Rows[dgvSubDetail.CurrentCell.RowIndex].Cells[j];
                    }

                    return true;
                    #endregion
            }
            return false;
        }
        private bool Update_SubDetail(DataGridViewRow dgvRow)
        {
            if (dgvRow == null) return false;
            string strSQL = string.Empty;
            this.dgvSubDetail.EndEdit();
            DBAccess dbcon = new DBAccess(false, false);
            string strFields = "SeqNo,CategoryID,CategoryName,WKProductID,WKName,WorkCostRate,WKPriceType,WKSTDQty,WKSalary,WKSTDSalary,STDQtyUnit,WKUnitCost,WorkersQty,WKTotUnitCost,WorkPositionSeqNo,HdrRecID";
            try
            {
                DataRow drDtl = dbcon.GetDataTable("select top 0 * from " + mstrTableName_Sub + " ").NewRow();
                if (CvtFunc.ToDouble(dgvRow.Cells[(int)DtlCol.RecID].Value) > 0)   //Update
                {
                    BaseFunctions.FillDataRowByGridRow(dgvRow, drDtl);
                    DataFunctions.UpdateTable(dbcon, strFields, mstrTableName_Sub, drDtl, FormStatus.Update);
                }
                else //Add New
                {
                    BaseFunctions.FillDataRowByGridRow(dgvRow, drDtl);
                    DataFunctions.UpdateTable(dbcon, strFields, mstrTableName_Sub, drDtl, FormStatus.AddNew);
                    dgvRow.Cells[(int)DtlCol.RecID].Value = CvtFunc.ToString(drDtl["RecID"]);
                }

                #region 计算 Hdr表的固定工资和标准工资
                ReCalHdrCost(dbcon);
                #endregion
                dbcon.CloseCon();
            }
            catch (Exception ex)
            {
                dbcon.CloseCon();
                return false;
            }
            return true;
        }

        /// <summary>
        /// 计算 Hdr表的固定工资和标准工资
        /// </summary>
        /// <param name="dbcon"></param>
        private void  ReCalHdrCost( DBAccess dbcon)
        {
            
            string strHdrRecID = CvtFunc.ToString(this.dgvDetail.CurrentRow.Cells[(int)DtlCol.RecID].Value);
            string strSQL = "select sum(WKSalary*WorkersQty) as WKSalary, sum(WKSTDSalary*WorkersQty) as WKSTDSalary from imCategoryWKWorkCostPosition Where HdrRecID='" + strHdrRecID + "'";
            DataRow drCal = dbcon.GetDataFirstRow(strSQL);
            if (drCal != null && !mblnInDetailMode)
            {
                this.dgvDetail.CurrentRow.Cells[(int)DtlCol.WKSalary].Value = BaseFunctions.FormatNumeric(CvtFunc.ToDouble(drCal["WKSalary"]), "ns.2");
                this.dgvDetail.CurrentRow.Cells[(int)DtlCol.WKSTDSalary].Value = BaseFunctions.FormatNumeric(CvtFunc.ToDouble(drCal["WKSTDSalary"]), "ns.2");
            }
            else
            {
                if (mblnInDetailMode)
                {
                    strHdrRecID = CvtFunc.ToString(this.dgvSubDetail.CurrentRow.Cells[(int)DtlCol.HdrRecID].Value);
                    strSQL = "select sum(WKSalary*WorkersQty) as WKSalary, sum(WKSTDSalary*WorkersQty) as WKSTDSalary from imCategoryWKWorkCostPosition Where HdrRecID='" + strHdrRecID + "'";
                    drCal = dbcon.GetDataFirstRow(strSQL);
                    foreach (DataGridViewRow dgvRow in this.dgvDetail.Rows)
                    {
                        if (strHdrRecID == CvtFunc.ToString(dgvRow.Cells[(int)DtlCol.RecID].Value))
                        {
                            dgvRow.Cells[(int)DtlCol.WKSalary].Value = BaseFunctions.FormatNumeric(CvtFunc.ToDouble(drCal["WKSalary"]), "ns.2");
                            dgvRow.Cells[(int)DtlCol.WKSTDSalary].Value = BaseFunctions.FormatNumeric(CvtFunc.ToDouble(drCal["WKSTDSalary"]), "ns.2");
                            Update_Detail(dgvRow);
                            break;
                        }
                    }
                }
            }
        }

        private bool Delete_SubDetail(DataGridViewRow dgvRow)
        {
            if (dgvRow == null) return true;
            string strRecID = CvtFunc.ToString(dgvRow.Cells[(int)DtlCol.RecID].Value);
            string strSeqNo = CvtFunc.ToString(dgvRow.Cells[(int)DtlCol.RowSeqNo].Value);
            string strSQL = string.Empty;
            if (CvtFunc.ToDouble(strRecID) > 0)
            {
                if (MessageBox.Show("您确定要删除？", "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    DBAccess dbcon = new DBAccess(false, false);
                    try
                    {
                        dbcon.BeginTran();
                        strSQL = "Delete " + mstrTableName_Sub + " where RecID='" + strRecID + "'";
                        dbcon.ExecuteSql(strSQL);
                        dbcon.CommitTran();

                        ReCalHdrCost(dbcon);
                        //string strWKProductID = CvtFunc.ToString(this.dgvDetail.CurrentRow.Cells[(int)DtlCol.WKProductID].Value);
                        //string strWKPriceType = CvtFunc.ToString(this.dgvDetail.CurrentRow.Cells[(int)DtlCol.WKPriceType].Value);
                        //strSQL = "select * from imCategoryWKWorkCost where WKProductID='" + strWKProductID + "' and WKPriceType='" + strWKPriceType + "'";
                        //DataTable dtFind = dbcon.GetDataTable(strSQL);
                        //if (dtFind.Rows.Count > 0)
                        //{
                        //    if (MessageBox.Show("其他类别有相同的费用标准，是否也删除？", "", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes)
                        //    {
                        //        foreach (DataRow drRow in dtFind.Rows)
                        //        {
                        //            strSQL = "Delete " + mstrTableName + " where RecID='" + drRow["RecID"] + "'";
                        //            dbcon.ExecuteSql(strSQL);
                        //        }
                        //    }
                        //}
                    }
                    catch (Exception ex)
                    {
                        dbcon.RollbackTran();
                    }
                    dbcon.CloseCon();
                }
            }
            return true;
        }
        private void dgvSubDetail_CellLeave(object sender, DataGridViewCellEventArgs e)
        {
            this.dgvSubDetail.EndEdit();

            if (   e.ColumnIndex == (int)DtlCol.WKName || e.ColumnIndex == (int)DtlCol.WorkCost
                || e.ColumnIndex ==(int)DtlCol.WorkPositionSeqNo || e.ColumnIndex ==(int)DtlCol.WKPostiton || e.ColumnIndex ==(int)DtlCol.WKSalary )
            {
                string strErrMsg = string.Empty;
                if (CheckSubDtlRow(ref strErrMsg))
                {
                    Update_SubDetail(this.dgvSubDetail.Rows[e.RowIndex]);
                }
                else
                {
                    
                }
            }
            if (e.ColumnIndex == (int)DtlCol.WKName)
            {
                this.dgvSubDetail.Rows[e.RowIndex].Cells[(int)DtlCol.WKRemark].Value = DataFunctions.GetFieldValue("rmWorkingProcedure", "Remark", "WorkingProcedureName='" + CvtFunc.ToString(this.dgvSubDetail.CurrentRow.Cells[(int)DtlCol.WKName].Value) + "'");
            }
            else if (e.ColumnIndex == (int)DtlCol.WorkCost)
            {
                this.dgvSubDetail.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = BaseFunctions.FormatNumeric(this.dgvSubDetail.Rows[e.RowIndex].Cells[e.ColumnIndex].Value, "Ns.2");
            }
        }

        #endregion

        private void btnExit_Click(object sender, EventArgs e)
        {
            mPressExit = true;
            this.Close();
        }

        private void btnHelp_Click(object sender, EventArgs e)
        {
            mPressExit = true;
            base.OnHelp();
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            string strErrMsg = string.Empty;
            if (CheckDtlRow(ref strErrMsg))
            {
                Update_Detail(this.dgvDetail.CurrentRow);
                if (CheckSubDtlRow(ref strErrMsg))
                {
                    Update_SubDetail(this.dgvSubDetail.CurrentRow);
                }
                else
                {
                    MsgBoxEx.Show(strErrMsg); 
                }
                //this.Close();
            }
        }

        private void btnToExcel_Click(object sender, EventArgs e)
        {
//            string strSQL = @"select WKName as  工序名称,CategoryName as 类别名称,WorkCostRate as 工时费用标准,WKPriceType as 工价类别,
//                             WKSTDQty as 额定产能,WKSalary as 固定工资,WKSTDSalary as 标准工资,WKUnitCost 计件工价,RecID from imCategoryWKWorkCost ";
//            DataView dv = mdbcon.GetDataTable ( strSQL ).DefaultView;
            ToExcel.DataGirdViewToExcel( this.dgvDetail);

            
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {            
            FillDtl("ALL");
        }

        private void btn_CopyTo_Click(object sender, EventArgs e)
        {
            string strSQL = "";
            string strFindValue = string.Empty;
            string strFieldName = string.Empty;
            string strSql = string.Empty;
            string strCaption = string.Empty;
            string strDestCategory = "";
            string strDestCategoryID = "";
            string strOrigCategory = "";
            string strOrigCategoryID = "";

            if (this.dgvDetail.CurrentRow == null) return;
            this.Update_Detail(this.dgvDetail.CurrentRow); 
            strOrigCategoryID = CvtFunc.ToString(this.dgvDetail.CurrentRow.Cells[(int)DtlCol.CategoryID].Value);
            strOrigCategory = CvtFunc.ToString(this.dgvDetail.CurrentRow.Cells[(int)DtlCol.CategoryName].Value);
            strFindValue = strOrigCategory;
            strFieldName = "CategoryID";
            strCaption = "复制  类别名称：" + strOrigCategory + " 到 ";
            strSql = BaseFunctions.GetLookUpSQL("rmCategory");

            if (mblnLookUpSortByDescription) strSql = DataFunctions.GetSqlByDescriptionSort(strSql);
            mblnLookUpSortByDescription = false;

            frmLookUp frmlookup = new frmLookUp(strSql, strCaption, strFindValue, strFieldName);
            frmlookup.ShowDialog();
            if (frmlookup.HaveReturnValue)
            {
                strDestCategoryID = frmlookup.GetReturnValue(strFieldName);
                strDestCategory = frmlookup.GetReturnValue("CategoryName");
            }
            else 
            {
                return;
            }
            frmlookup.Close();

            if (strDestCategoryID == strOrigCategoryID)
            {
                MsgBoxEx.Show("目标类别不能和源类别相同", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else
            {
                strSQL = " insert into imCategoryWKWorkCost (WKProductID,WKName,CategoryID,CategoryName,WorkCostRate,WKPriceType,WKSTDQty,WKSalary,WKSTDSalary,STDQtyUnit) ";
                strSQL += "select WKProductID,WKName,'" + strDestCategoryID + "','" + strDestCategory + "',WorkCostRate,WKPriceType,WKSTDQty,WKSalary,WKSTDSalary,STDQtyUnit from imCategoryWKWorkCost where categoryID='" + strOrigCategoryID + "'";
                strSQL += " and WKName not in (select WKName from imCategoryWKWorkCost where categoryID='" + strDestCategoryID + "')";
                DBAccess dbcon = new DBAccess(false, false);
                dbcon.ExecuteSql(strSQL);
                dbcon.CloseCon();                
                FillDtl("ALL");                
            }
        }

        private void btnAddLine_Click(object sender, EventArgs e)
        {
            if (mstrCategoryID == "")
            {
                MsgBoxEx.Show("请选择类别", "PDM"); 
                return;
            }
            string strErrMsg = "";
            if (dgvDetail.CurrentRow == null)
            {
                AddDtlRow(this.dgvDetail.Name);
                return;
            }
            if (CvtFunc.ToInt(dgvDetail.CurrentRow.Cells[(int)DtlCol.RecID].Value) > 0)
            {
                if (CvtFunc.ToInt(dgvDetail.CurrentRow.Cells[(int)DtlCol.ValueChanged].Value) > 0)
                {

                    if (CheckDtlRow(ref strErrMsg))
                    {

                        if (!Update_Detail(this.dgvDetail.Rows[this.dgvDetail.CurrentCell.RowIndex]))
                        {
                            return ;
                        }
                    }
                    else
                    {
                        MessageBox.Show(strErrMsg, SysVar.WarnTitle, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        return ;
                    }
                }
                if (dgvDetail.CurrentCell.RowIndex < dgvDetail.Rows.Count - 1)
                {
                    dgvDetail.CurrentCell = this.dgvDetail.Rows[dgvDetail.CurrentCell.RowIndex + 1].Cells[(int)DtlCol.WKName];
                }
               
                {
                    AddDtlRow(this.dgvDetail.Name);
                }
            }
            else
            {
                if (CheckDtlRow(ref strErrMsg))
                {
                    if (!Update_Detail(this.dgvDetail.Rows[this.dgvDetail.CurrentCell.RowIndex]))
                    {
                        return ;
                    }
                    AddDtlRow(this.dgvDetail.Name);
                }
                else
                {
                    MessageBox.Show(strErrMsg, SysVar.WarnTitle, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    return ;
                }
            }
            
        }

        private void FillTree()
        {
            DBAccess dbcon = new DBAccess(false, false);
            string strSQL = "select CategoryID,CategoryName from rmCategory order by CategoryID";
            DataTable dtItem = dbcon.GetDataTable(strSQL);
            foreach (DataRow drType in dtItem.Rows)
            {
                TreeNode tvNode = new TreeNode(CvtFunc.ToString(drType["CategoryID"]) + "-" + CvtFunc.ToString(drType["CategoryName"]));
                this.tvTree.Nodes.Add(tvNode);
                int intRow = 0;
                strSQL = "select distinct B.WorkingProcedureID,B.WorkingProcedureName from imCategoryWKWorkCost A inner join rmWorkingProcedure B on A.WKProductID=B.WorkingProcedureID" +
                         " where CategoryID='" + CvtFunc.ToString(drType["CategoryID"]) + "'";
                DataTable dtDtl = dbcon.GetDataTable(strSQL);
                foreach (DataRow drDtl in dtDtl.Rows)
                {
                    tvNode.Nodes.Add(CvtFunc.ToString(drDtl["WorkingProcedureID"]) + "-" + CvtFunc.ToString(drDtl["WorkingProcedureName"]));
                }
            }
            dbcon.CloseCon();
        }

        private void tvTree_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (e.Node.Nodes.Count == 0) //最后一层
            {
                if (e.Node.Parent != null)
                {
                    string strCategoryID = CvtFunc.ToString(e.Node.Parent.Text).Split('-')[0];
                    string strCond = " CategoryID ='" + strCategoryID + "'";

                    string strWKProductID = "";
                    strWKProductID = CvtFunc.ToString(e.Node.Text.ToString()).Split('-')[0];
                    strCond += "and WKProductID ='" + strWKProductID + "'";
                    mstrCategoryID = strCategoryID;
                    mstrWKProductID = strWKProductID;
                    FillDtl(strCond);
                }
                else
                {
                    string strCategoryID = "";
                    strCategoryID = CvtFunc.ToString(e.Node.Text.ToString()).Split('-')[0];
                    string strCond = " CategoryID ='" + strCategoryID + "'";
                    mstrCategoryID = strCategoryID;
                    mstrWKProductID = ""; 
                    FillDtl(strCond);
                    
                }
            }
            else  //最上层
            {
                string strCategoryID = "";
                strCategoryID = CvtFunc.ToString(e.Node.Text.ToString()).Split('-')[0];
                string strCond = " CategoryID ='" + strCategoryID + "'";
                mstrCategoryID = strCategoryID;
                mstrWKProductID = ""; 
                FillDtl(strCond);                
            }
        }

        private void frmImCategroyWKWorkCost_Load(object sender, EventArgs e)
        {
            FillTree();
            FillDtl("ALL"); 
        }

        private void btnUpdateOrInsertToAllCate_Click(object sender, EventArgs e)
        {
            if (this.dgvDetail.CurrentRow == null) return;
            this.Update_Detail(this.dgvDetail.CurrentRow); 

            string strSQL = string.Empty;
            DBAccess dbcon = new DBAccess ( false,false );
            if (MessageBox.Show("更新/添加 此行设置到所有类别？", "PDM", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes)
            {
                string strWKProductID = CvtFunc.ToString ( this.dgvDetail.CurrentRow.Cells[(int)DtlCol.WKProductID].Value);
                string strWKPriceType = CvtFunc.ToString ( this.dgvDetail.CurrentRow.Cells[(int)DtlCol.WKPriceType].Value);
                string strRecID = CvtFunc.ToString(this.dgvDetail.CurrentRow.Cells[(int)DtlCol.RecID].Value); 
                strSQL = "select CategoryID,CategoryName from rmCategory order by CategoryID";
                DataTable dtItem = dbcon.GetDataTable(strSQL);
                foreach ( DataRow drRow in dtItem.Rows )
                {
                    string strDestCategoryID = CvtFunc.ToString ( drRow ["CategoryID"]);
                    string strDestCategory = CvtFunc.ToString (drRow["DestCategory"]);

                    DataRow drFind = dbcon.GetDataFirstRow("select recid from imCategoryWKWorkCost where CategoryID='" + strDestCategoryID + "' and WKProductID='" + strWKProductID + "' and WKPriceType='" + strWKPriceType + "'");
                    if (drFind == null)  //没有就添加
                    {
                        strSQL = " insert into imCategoryWKWorkCost (WKProductID,WKName,CategoryID,CategoryName,WorkCostRate,WKPriceType,WKSTDQty,WKSalary,WKSTDSalary,STDQtyUnit,Remark) ";
                        strSQL += "select WKProductID,WKName,'" + strDestCategoryID + "','" + strDestCategory + "',WorkCostRate,WKPriceType,WKSTDQty,WKSalary,WKSTDSalary,STDQtyUnit,Remark from imCategoryWKWorkCost where RecID='" + strRecID + "'";
                    }
                    else //也就更新
                    {
                        strSQL = "update imCategoryWKWorkCost set WorkCostRate='{0}' ,WKSTDQty='{1}',WKSalary='{2}',WKSTDSalary='{3}',STDQtyUnit='{4}' where RecID='" + drFind["RecID"] + "'";
                        strSQL = string.Format(strSQL, CvtFunc.ToString(this.dgvDetail.CurrentRow.Cells[(int)DtlCol.WorkCost].Value),
                                                         CvtFunc.ToString(this.dgvDetail.CurrentRow.Cells[(int)DtlCol.WKSTDQty].Value),
                                                         CvtFunc.ToString(this.dgvDetail.CurrentRow.Cells[(int)DtlCol.WKSalary].Value),
                                                         CvtFunc.ToString(this.dgvDetail.CurrentRow.Cells[(int)DtlCol.WKSTDSalary].Value),
                                                         CvtFunc.ToString(this.dgvDetail.CurrentRow.Cells[(int)DtlCol.STDQtyUnit].Value));
                                                         
                    }                
                    dbcon.ExecuteSql(strSQL);
                }
            }
        }

        private void dgvSubDetail_Leave(object sender, EventArgs e)
        {
            string strErrMsg = string.Empty;
            if (CheckSubDtlRow(ref strErrMsg))
            {
                Update_SubDetail(this.dgvSubDetail.CurrentRow);
            }
        }

        private void btnShowAllDetail_Click(object sender, EventArgs e)
        {
            mblnInDetailMode = true;
            dgvDetail.Enabled = false;
            dgvSubDetail.Top = dgvDetail.Top;
            dgvSubDetail.Height = dgvDetail.Height + dgvSubDetail.Height + 10;
            FillSubDtl(-1); 
        }

     

    }
}
